在下面的代码中,j
是 data.frames 的命名列表。我想知道是否有办法:
(a)提取变量的数值(即one.short
and one.long
)在 data.frames 内并附加它们的相关名称(即"AAA"
or "BBB"
or "CCC"
)到提取的值?
(b)如果一个变量(即one.short
or one.long
) is NA
,然后将该 data.frame 的名称更改为NA
当提取该变量时?
预期输出:
在下面的示例中,我希望我的输出如下:
one.short = list(c("AAA", .6), c(NA, NA), c("CCC", .4))
one.long = list(c("AAA", .8), c(NA, NA), c(NA, NA))
R 代码和可重现的数据:
j <- list(data.frame(one.short = .6, one.long = .8), data.frame(one.short = NA,
one.long = NA), data.frame(one.short = .4, one.long = NA))
names(j) <- c("AAA", "BBB", "CCC")
# I tried this without success:
one.short = sapply(1:length(j), function(i) j[[i]]$one.short)
one.long = sapply(1:length(j), function(i) j[[i]]$one.long)