开始使用免费开始使用

创建带名称的列表

很好,你已经越来越熟练了!

就像整理待办事项一样,你肯定希望能知道或记得列表中的各个组件分别代表什么。这就是为什么你应该为它们添加名称:

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
编辑并运行代码