Read in 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")

Question 1

m.names[1:10]
##  [1] "Baramgwa hamjje sarajida"     "Sleepless in Seattle"        
##  [3] "The Water Diviner"            "Fly Away Home"               
##  [5] "The Three Musketeers"         "Candyman: Farewell to Flesh" 
##  [7] "Honey I Blew Up the Kid"      "Kingsman: The Secret Service"
##  [9] "Ajab Prem Ki Ghazab Kahani"   "A Bug's Life"

Question 2

A

genre[1:5]
## [1] "Action"          "Romantic Comedy" "Drama"           "Drama"          
## [5] "Adventure"

B

time[1:5]
## [1] 121 100 112  NA  NA

Question 3

m.names[seq(from = 1, to = 20, by = 2)]
##  [1] "Baramgwa hamjje sarajida"   "The Water Diviner"         
##  [3] "The Three Musketeers"       "Honey I Blew Up the Kid"   
##  [5] "Ajab Prem Ki Ghazab Kahani" "Courage Under Fire"        
##  [7] "In the Name of the Father"  "Magnum Force"              
##  [9] "House of Sand and Fog"      "Jackass 3D"

Question 4

where.is.name  <- m.names %in% "Tropic Thunder - A Pirate's Tale"
Where.is.name <- m.names == "Tropic Thunder - A Pirate's Tale"

m.names [m.names == "Tropic Thunder - A Pirate's Tale"] <- "Tropic Thunder"

Question 5

Change the genre names “Romantic Comedy” to “RomCom”. Change the genre name “Horror” to “Scary movie!!!”

change.genre <- genre %in% "Romantic Comedy"
change.genre <- genre == "Romantic Comedy"

genre[genre == "Romantic Comedy"] <- "RomCom"

genre.change <- genre %in% "Horror"
genre.change <- genre == "Horror"
genre[genre == "Horror"] <- "Scary movie!!!"

Question 6

Create a new vector called “boxoffice.millions” that has the box-office values in millions of dollars. For example, a value of 1000000 in the original boxoffice vector should be 1 in boxoffice.millions

boxoffice.millions <- boxoffice/1000000
boxoffice.millions
##  [1]  28.68654 218.07602  30.86465  35.87084  50.37563  13.89954  58.66245
##  [8] 404.56172  15.90641 363.08943 100.74826  14.15675  25.09686  14.55381
## [15]  44.68047  89.17749  16.15792  26.32427 171.68579 191.09125

Question 7

What is the mean, median, and standard deviation of the box-office totals of all movies?

mean(boxoffice)
## [1] 95683306
median(boxoffice)
## [1] 40275655
sd(boxoffice)
## [1] 116422556

Question 8

table(genre)
## genre
##         Action      Adventure         Comedy          Drama         RomCom 
##              3              2              5              7              2 
## Scary movie!!! 
##              1

Question 9

How many movies were Dramas? (hint: don’t use table(), use sum())

sum (genre == "Drama")
## [1] 7

Question 10

What was the box-office total, genre, running time, and rating of “A Bug’s Life”? (use indexing, don’t look up the values visually)

what.was <- m.names == "A Bug's Life"
genre[what.was]
## [1] "Adventure"
# OR

unique(genre[m.names == "A Bug's Life"])
## [1] "Adventure"
unique(boxoffice[m.names == "A Bug's Life"])
## [1] 363089431
unique(time[m.names == "A Bug's Life"])
## [1] 96
unique(rating [m.names == "A Bug's Life"])
## [1] "G"

Question 11

Is the movie “Pirate’s of the Caribbean” in the list? (hint: use a combination of logical indexing and the sum() function)

"Pirate's of the Caribbean " %in% m.names
## [1] FALSE
# OR

sum( m.names == "Pirate's of the Caribbean")
## [1] 0

Question 12

  1. What were the names of the Comedy movies? B) What was the mean box office revenue of those movies?
# A
unique(m.names[genre == "Comedy"])
## [1] "Honey I Blew Up the Kid"    "Ajab Prem Ki Ghazab Kahani"
## [3] "Soul Plane"                 "Jackass 3D"                
## [5] "Tropic Thunder"
# B

mean(boxoffice[m.names %in% unique(m.names[genre == "Comedy"])])
## [1] 90379943

Question 13

What were the names of those movies that made at least $50 Million dollars?

at.least <- (boxoffice >= 50000000)
at.least
##  [1] FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
## [12] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE
m.names[at.least]
## [1] "Sleepless in Seattle"         "The Three Musketeers"        
## [3] "Honey I Blew Up the Kid"      "Kingsman: The Secret Service"
## [5] "A Bug's Life"                 "Courage Under Fire"          
## [7] "About Time"                   "Jackass 3D"                  
## [9] "Tropic Thunder"

¦ Question 14

  1. Out of all the movies that were either Comedies or Dramas, what was the smallest box office revenue? (Hint: use the %in% function.) B) What was the name of that movie? (Hint: Use logical indexing based on what you found in part A).
# A
Drama.comedy <- boxoffice[genre %in% c("Drama", "Comedy")]
Drama.comedy
##  [1]  30864649  35870837  58662452  15906411 100748262  14156753  25096862
##  [8]  14553807  16157923  26324268 171685793 191091250
min(Drama.comedy)
## [1] 14156753
# OR
min(boxoffice[genre %in% c("Drama", "Comedy")])
## [1] 14156753
# B

m.names[boxoffice == min(boxoffice[genre %in% c("Drama", "Comedy")])]
## [1] "Dirty Pretty Things"

Question 15

  1. What was the median movie time in minutes? B) What was the median movie time in hours?
# A
time.finite <- time[is.finite(time)]
time.finite
##  [1] 121 100 112 129  96 111 123 121  93 106
median(time.finite)
## [1] 111.5
# B
median(time.finite/60)
## [1] 1.858333