為全球票房新增一個欄
在前一題中,你已經計算出一個向量,裡面包含三部 Star Wars 電影各自的全球票房。不過,這個向量尚未加入 star_wars_matrix。
你可以用 cbind() 函式把一或多個欄位加到矩陣中。它會依欄把矩陣與/或向量合併在一起。例如:
big_matrix <- cbind(matrix1, matrix2, vector1 ...)
本練習屬於課程
R 入門
練習說明
將 worldwide_vector 作為新的欄加入 star_wars_matrix,並把結果指定給 all_wars_matrix。請使用 cbind() 函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Construct star_wars_matrix
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
region <- c("US", "non-US")
titles <- c("A New Hope",
"The Empire Strikes Back",
"Return of the Jedi")
star_wars_matrix <- matrix(box_office,
nrow = 3, byrow = TRUE,
dimnames = list(titles, region))
# The worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)
# Bind the new variable worldwide_vector as a column to star_wars_matrix
all_wars_matrix <-