我有一个嵌套列表,需要计算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)
不起作用
答案 0 :(得分:3)
> 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
答案 1 :(得分:1)
你想使用ldply
,它将你的功能应用于列表的元素,然后像你的ddply
调用的第二个版本应该有效。没有示例数据,我无法证明它。
答案 2 :(得分:1)
ddply
明确地设计用于处理data.frame。所以,我首先将列表的所有元素放在data.frame
:
dat = do.call("rbind", nested_list)
然后使用ddply
:
ddply(dat, .(fapp, sapp), corr)