This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Insert the data
m.names <- c("Baramgwa hamjje sarajida", "Sleepless in Seattle", "The Water Diviner",
"Fly Away Home", "The Three Musketeers", "Candyman: Farewell to Flesh",
"Honey I Blew Up the Kid", "Kingsman: The Secret Service", "Ajab Prem Ki Ghazab Kahani",
"A Bug's Life", "Courage Under Fire", "Dirty Pretty Things",
"In the Name of the Father", "Soul Plane", "Magnum Force", "About Time",
"House of Sand and Fog", "Bokura ga ita Zenpen", "Jackass 3D",
"Tropic Thunder - A Pirate's Tale")
boxoffice <- c(28686545, 218076024, 30864649, 35870837, 50375628, 13899536,
58662452, 404561724, 15906411, 363089431, 100748262, 14156753,
25096862, 14553807, 44680473, 89177486, 16157923, 26324268, 171685793,
191091250)
genre <- c("Action", "Romantic Comedy", "Drama", "Drama", "Adventure",
"Horror", "Comedy", "Action", "Comedy", "Adventure", "Drama",
"Drama", "Drama", "Comedy", "Action", "Romantic Comedy", "Drama",
"Drama", "Comedy", "Comedy")
time <- c(121, 100, 112, NA, NA, NA, NA, 129, NA, 96, 111, NA, NA, NA,
NA, 123, NA, 121, 93, 106)
rating <- c(NA, "PG", "R", "PG", "PG", "R", "PG", "R", NA, "G", "R",
"R", "R", "R", NA, "R", "R", NA, "R", "R")
Question1:
movies.sample <-data.frame(m.names, boxoffice,genre, time, rating)
movies.sample
## m.names boxoffice genre time rating
## 1 Baramgwa hamjje sarajida 28686545 Action 121 <NA>
## 2 Sleepless in Seattle 218076024 Romantic Comedy 100 PG
## 3 The Water Diviner 30864649 Drama 112 R
## 4 Fly Away Home 35870837 Drama NA PG
## 5 The Three Musketeers 50375628 Adventure NA PG
## 6 Candyman: Farewell to Flesh 13899536 Horror NA R
## 7 Honey I Blew Up the Kid 58662452 Comedy NA PG
## 8 Kingsman: The Secret Service 404561724 Action 129 R
## 9 Ajab Prem Ki Ghazab Kahani 15906411 Comedy NA <NA>
## 10 A Bug's Life 363089431 Adventure 96 G
## 11 Courage Under Fire 100748262 Drama 111 R
## 12 Dirty Pretty Things 14156753 Drama NA R
## 13 In the Name of the Father 25096862 Drama NA R
## 14 Soul Plane 14553807 Comedy NA R
## 15 Magnum Force 44680473 Action NA <NA>
## 16 About Time 89177486 Romantic Comedy 123 R
## 17 House of Sand and Fog 16157923 Drama NA R
## 18 Bokura ga ita Zenpen 26324268 Drama 121 <NA>
## 19 Jackass 3D 171685793 Comedy 93 R
## 20 Tropic Thunder - A Pirate's Tale 191091250 Comedy 106 R
Question2
#A
nrow(movies.sample)
## [1] 20
ncol(movies.sample)
## [1] 5
#B
names(movies.sample)
## [1] "m.names" "boxoffice" "genre" "time" "rating"
Question3
#A
head(movies.sample)
## m.names boxoffice genre time rating
## 1 Baramgwa hamjje sarajida 28686545 Action 121 <NA>
## 2 Sleepless in Seattle 218076024 Romantic Comedy 100 PG
## 3 The Water Diviner 30864649 Drama 112 R
## 4 Fly Away Home 35870837 Drama NA PG
## 5 The Three Musketeers 50375628 Adventure NA PG
## 6 Candyman: Farewell to Flesh 13899536 Horror NA R
#B
tail(movies.sample)
## m.names boxoffice genre time rating
## 15 Magnum Force 44680473 Action NA <NA>
## 16 About Time 89177486 Romantic Comedy 123 R
## 17 House of Sand and Fog 16157923 Drama NA R
## 18 Bokura ga ita Zenpen 26324268 Drama 121 <NA>
## 19 Jackass 3D 171685793 Comedy 93 R
## 20 Tropic Thunder - A Pirate's Tale 191091250 Comedy 106 R
#C
movies<-read.table(file="http://nathanieldphillips.com/wp-content/uploads/2015/11/movies1.txt", header=T, sep = "\t", stringsAsFactors = F)
movies[20:30, ]
## name boxoffice.total
## 20 Star Wars Ep. I: The Phantom Menace 1.027e+09
## 21 Alice in Wonderland 1.025e+09
## 22 The Hobbit: An Unexpected Journey 1.017e+09
## 23 The Dark Knight 1.003e+09
## 24 The Lion King 9.875e+08
## 25 Despicable Me 2 9.749e+08
## 26 Harry Potter and the SorcererÕs Stone 9.748e+08
## 27 Pirates of the Caribbean: At World's End 9.634e+08
## 28 The Hobbit: The Desolation of Smaug 9.604e+08
## 29 Harry Potter and the Deathly Hallows: Part I 9.593e+08
## 30 The Hobbit: The Battle of the Five Armies 9.551e+08
## boxoffice.domestic boxoffice.international dvd.domestic budget
## 20 474544677 552500000 NA 115000000
## 21 334191110 691300000 82297116 200000000
## 22 303003568 714000000 34191124 250000000
## 23 533345358 469546000 282134660 185000000
## 24 422780140 564700000 86246006 79300000
## 25 368065385 606808379 124381409 76000000
## 26 317575550 657179821 NA 125000000
## 27 309420425 654000000 312228146 300000000
## 28 258366855 702000000 40247146 250000000
## 29 295001070 664300000 94385569 125000000
## 30 255119788 700000000 22439417 250000000
## rating genre creative.type time year production.method
## 20 PG Adventure Science Fiction 133 1999 Animation/Live Action
## 21 PG Adventure Fantasy 108 2010 Animation/Live Action
## 22 PG-13 Adventure Fantasy 169 2012 Animation/Live Action
## 23 PG-13 Action Super Hero 152 2008 Live Action
## 24 G Adventure Kids Fiction 87 1994 Hand Animation
## 25 PG Comedy Kids Fiction 98 2013 Digital Animation
## 26 PG Adventure Fantasy 152 2001 Animation/Live Action
## 27 PG-13 Adventure Historical Fiction 167 2007 Live Action
## 28 PG-13 Adventure Fantasy 201 2013 Animation/Live Action
## 29 PG-13 Adventure Fantasy 146 2010 Animation/Live Action
## 30 PG-13 Adventure Fantasy 144 2014 Live Action
## sequel boxoffice.domestic.inflationadj budget.millions
## 20 1 755899504 115.0
## 21 0 354522122 200.0
## 22 1 317749322 250.0
## 23 1 621624466 185.0
## 24 0 756219975 79.3
## 25 1 378930222 76.0
## 26 0 468612897 125.0
## 27 1 376431535 300.0
## 28 1 265846191 250.0
## 29 1 312886528 125.0
## 30 1 260520067 250.0
## boxoffice.total.millions revenue.d.budget
## 20 1027.0 8.931
## 21 1025.5 5.127
## 22 1017.0 4.068
## 23 1002.9 5.421
## 24 987.5 12.452
## 25 974.9 12.827
## 26 974.8 7.798
## 27 963.4 3.211
## 28 960.4 3.841
## 29 959.3 7.674
## 30 955.1 3.820
Question4
#A
Harry.Potter <- subset(x = movies, subset = (name == "Harry Potter and the Chamber of Secrets"))
Harry.Potter
## name boxoffice.total
## 40 Harry Potter and the Chamber of Secrets 8.79e+08
## boxoffice.domestic boxoffice.international dvd.domestic budget
## 40 261987880 616991754 NA 100000000
## rating genre creative.type time year production.method sequel
## 40 PG Adventure Fantasy 161 2002 Animation/Live Action 1
## boxoffice.domestic.inflationadj budget.millions
## 40 376584346 100
## boxoffice.total.millions revenue.d.budget
## 40 879 8.79
#B
Harry.Potter["rating"]
## rating
## 40 PG
#C
Harry.Potter7 <- subset(x = movies, subset = (name == "Harry Potter and the Deathly Hallows: Part II"))
Harry.Potter7["boxoffice.total"]
## boxoffice.total
## 7 1.342e+09
#D
HarryPotter5 <- subset(x = movies, subset = (name == "Harry Potter and the Order of the Phoenix"))
HarryPotter5["time"]
## time
## 31 138
Question5
index <- order(df$col.to.sort, decreasing = T)
df <- df[index,]
#A
index <- order(movies$year, decreasing = F)
#B
new.order<-movies[index,]
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.