gpt4 book ai didi

r - 将标准偏差应用于按因素拆分的数据框

转载 作者:行者123 更新时间:2023-12-02 07:31:23 24 4
gpt4 key购买 nike

我正在尝试将 sd 函数应用于我的数据框,但它不起作用:

sdsd <- by(nowna[, 1:16], nowna$stamm, sd)
Error in FUN(X[[1L]], ...) : could not find function "FUN"

你知道为什么吗?

非常感谢。

最佳答案

library(plyr)
dt <- data.frame(age=rchisq(20,10),group=sample(1:2,20,rep=T))

dt
age group
1 9.908015 2
2 11.415043 2
3 7.849433 2
4 8.850696 2
5 6.194783 2
6 11.111339 2
7 9.789127 2
8 10.844352 1
9 8.686503 2
10 21.579142 2
11 11.750417 1
12 3.719226 1
13 12.086820 1
14 13.562351 1
15 4.636543 2
16 12.648083 1
17 10.780387 2
18 10.651318 2
19 5.976533 1
20 13.546345 2

ddply(dt,~group,summarise,mean=mean(age),sd=sd(age))
group mean sd
1 1 10.08397 3.728750
2 2 10.38451 4.082198

使用新包 data.table 的另一种行变体。

dtf <- data.frame(age=rchisq(100000,10),group=factor(sample(1:10,100000,rep=T)))
dt <- data.table(dt)
dt[,list(mean=mean(age),sd=sd(age)),by=group]

使用聚合函数

aggregate(dt$age, by=list(dt$group), FUN=sd)
Group.1 x
1 1 3.728750
2 2 4.082198

关于r - 将标准偏差应用于按因素拆分的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382126/

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