gpt4 book ai didi

r - 如何ddply嵌套列表

转载 作者:行者123 更新时间:2023-12-04 11:26:38 26 4
gpt4 key购买 nike

我有一个嵌套列表,需要计算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)

没用

最佳答案

> 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

关于r - 如何ddply嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12898789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com