Getting mean and standard deviation from groups in a data.frame
15,104
Solution 1
Assuming your data is in a data.frame called 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
Solution 2
Another solution using ave
:
ave(DF$HR, DF$Group)
gives the mean and
ave(DF$HR, DF$Group, FUN=sd)
with DF being your data frame.
Author by
user2425547
Updated on June 05, 2022Comments
-
user2425547 almost 2 years
I have heart rate data in the form of a list with the four categories 1AS, 1CS, 1AI, 1CI each of variable size. I would like to output mean and standard deviations for each category in the list. I have the data in this format to calculate ANOVA and Tukey which I have done successfully but the mean has me stumped!
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