开始使用免费开始使用

为全球票房新增一列

在上一个练习中,您计算了一个向量,包含三部 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 <- 
编辑并运行代码