我有一个嵌套列表,需要计算frt和srt的相关性
$`bs. bs`
fapp frt sapp srt
1 bs 2280 bs 0.25
2 bs 2287 bs 0.25
3 bs 2288 bs 0.25
4 bs 2289 bs 0.25
$`bs. lhc`
fapp frt sapp srt
5 bs 2320 lhc 0.250000
6 bs 2333 lhc 0.250214
7 bs 2524 lhc 0.316449来源:
structure(list(`bs. bs` = structure(list(fapp = structure(c(1L,
1L, 1L, 1L), .Label = "bs", class = "factor"), frt = c(2280L,
2287L, 2288L, 2289L), sapp = structure(c(1L, 1L, 1L, 1L), .Label = c(" bs",
" lhc"), class = "factor"), srt = c(0.25, 0.25, 0.25, 0.25)), .Names = c("fapp",
"frt", "sapp", "srt"), row.names = c(NA, 4L), class = "data.frame"),
`bs. lhc` = structure(list(fapp = structure(c(1L, 1L, 1L), .Label = "bs", class= "factor"),
frt = c(2320L, 2333L, 2524L), sapp = structure(c(2L,
2L, 2L), .Label = c(" bs", " lhc"), class = "factor"),
srt = c(0.25, 0.250214, 0.316449)), .Names = c("fapp",
"frt", "sapp", "srt"), row.names = 5:7, class = "data.frame")), .Names = c("bs. bs",
"bs. lhc"))就像这样
ddply(y,.(fapp + sapp),cor)或
ddply(y,.(fapp,sapp),cor)不起作用
发布于 2012-10-15 23:45:17
> ldply(y, function(x) { x$corr <- cor(x$frt, x$srt); x })
.id fapp frt sapp srt corr
1 bs. bs bs 2280 bs 0.250000 NA
2 bs. bs bs 2287 bs 0.250000 NA
3 bs. bs bs 2288 bs 0.250000 NA
4 bs. bs bs 2289 bs 0.250000 NA
5 bs. lhc bs 2320 lhc 0.250000 0.9985343
6 bs. lhc bs 2333 lhc 0.250214 0.9985343
7 bs. lhc bs 2524 lhc 0.316449 0.9985343
Warning message:
In cor(x$frt, x$srt) : the standard deviation is zero
Calls: ldply -> llply -> structure -> lapply -> FUN -> cor或者将结果保存为列表。
> llply(y, function(x) { x$corr <- cor(x$frt, x$srt); x })
$`bs. bs`
fapp frt sapp srt corr
1 bs 2280 bs 0.25 NA
2 bs 2287 bs 0.25 NA
3 bs 2288 bs 0.25 NA
4 bs 2289 bs 0.25 NA
$`bs. lhc`
fapp frt sapp srt corr
5 bs 2320 lhc 0.250000 0.9985343
6 bs 2333 lhc 0.250214 0.9985343
7 bs 2524 lhc 0.316449 0.9985343
Warning message:
In cor(x$frt, x$srt) : the standard deviation is zero
Calls: llply -> structure -> lapply -> FUN -> cor发布于 2012-10-15 23:22:25
您希望使用ldply,它将您的函数应用于列表的元素,然后类似于对ddply的第二个版本的调用应该可以工作。没有示例数据,我无法对其进行演示。
发布于 2012-10-15 23:25:36
ddply是专门为处理data.frame而设计的。因此,我首先将列表的所有元素放在一个data.frame中
dat = do.call("rbind", nested_list)然后使用ddply
ddply(dat, .(fapp, sapp), corr)https://stackoverflow.com/questions/12898789
复制相似问题