开始使用免费开始使用

创建具名列表

做得好,状态正佳!

就像待办清单一样,您不希望忘记列表中各组件代表什么。因此,应该为它们命名:

my_list <- list(name1 = your_comp1, 
                name2 = your_comp2)

这会创建一个包含已命名组件的列表,组件名为 name1name2 等等。若想在创建列表后再命名组件,您可以像对向量那样使用 names() 函数。下面的命令与上面的赋值方式完全等价:

my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

本练习是课程的一部分

R 入门

查看课程

练习说明

  • 修改上一个练习的代码(见编辑器),为各组件添加名称。my_vector 使用名称 vecmy_matrix 使用名称 matmy_df 使用名称 df
  • 打印 my_list,以便查看输出。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Adapt list() call to give the components names
my_list <- list(my_vector, my_matrix, my_df)

# Print out my_list
编辑并运行代码