开始使用免费开始使用

添加一列全球票房数据

在上一个练习中,你计算出了一个包含三部《星球大战》电影各自全球票房收入的向量。不过,这个向量还没有成为 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 <- 
编辑并运行代码