如何命名列表元素

时间:2015-07-19 11:33:03

标签: r

我创建了一个列表,我想命名列表的元素。有人可以帮我吗?

LL <- vector(mode = "list")

L11 <- seq(1, 10)
L12 <- seq(11, 20)
LL[[length(LL)+1]] <- cbind(L11, L12)   
# I want to name LL[[1]] as "L1", but this does not work
names(LL[[length(LL)]]) <- c("L1")

L21 <- seq(21, 30)
L22 <- seq(31, 40)
LL[[length(LL)+1]] <- cbind(L21, L22)
# I want to name LL[[2]] as "L2", but this does not work
names(LL[[length(LL)]]) <- c("L2")

LL

2 个答案:

答案 0 :(得分:0)

这应该有效

LL <- vector(mode = "list") 
LL$l1 <- seq(1, 10)
LL$l2 <- seq(11, 20)

答案 1 :(得分:0)

根据David Arenburg(见上面的评论):

names(LL)[1L] <- "L1"

请参阅here了解其工作原理。