【发布时间】:2016-08-06 00:06:49
【问题描述】:
列表l 有三个字符串,分别命名为一、二和三。我想将l 转换为数据框,我需要一个附加列,其中包含n 中的名称。
l <- list(c("a", "b"), c("c", "d", "e"), c("e"))
n <- c("one", "two", "three")
我可以使用循环来做到这一点,但我确信有更有效的方法来做到这一点。
out <- NULL
for (i in 1:length(n)){
step <- rep(n[i], length(l[[i]]))
out <- c(out, step)}
df <- as.data.frame(unlist(l))
df$n <- out
df
# unlist(l) n
#1 a one
#2 b one
#3 c two
#4 d two
#5 e two
#6 e three
【问题讨论】: