获取data.frame中组的均值和标准差

时间:2013-05-27 15:35:24

标签: r statistics mean

我有一个列表形式的心率数据,每个类别有1AS,1CS,1AI,1CI可变大小。我想输出列表中每个类别的平均值和标准偏差。我有这种格式的数据来计算我已经成功完成的ANOVA和Tukey但是这个意思让我难过!

   Group  HR
1    1AS 300
2    1AS 280
3    1AS 260
4    1AS 250
5    1AS 300
6    1AS 272
7    1AS 250
8    1AS 198
9    1AS 200
10   1AS 195
11   1AS 214
12   1AS 249
13   1AS 240
14   1CS 250
15   1CS 236
16   1CS 200
17   1CS 272
18   1CS 206
19   1CS 203
20   1CS 237
21   1CS 214
22   1AI 218
23   1AI 276
24   1AI 240
25   1AI 264
26   1AI 300
27   1AI 315
28   1AI 300
29   1AI 285
30   1AI 286
31   1CI 167
32   1CI 233
33   1CI 214
34   1CI 219
35   1CI 214
36   1CI 246
37   1CI 230
38   1CI 218

2 个答案:

答案 0 :(得分:3)

假设您的数据位于名为DF的数据框中:

by(DF$HR,DF$Group,mean)

# DF$Group: 1AI
# [1] 276
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1AS
# [1] 246.7692
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1CI
# [1] 217.625
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1CS
# [1] 227.25

by(DF$HR,DF$Group,sd)

# DF$Group: 1AI
# [1] 30.93946
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1AS
# [1] 36.48551
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1CI
# [1] 23.25595
# ------------------------------------------------------------------------------------------------------------------------------------------------------------- 
# DF$Group: 1CS
# [1] 25.77236

答案 1 :(得分:2)

使用ave的另一种解决方案:

ave(DF$HR, DF$Group)

给出平均值和

ave(DF$HR, DF$Group, FUN=sd)

DF是您的数据框。