Using variations of `apply` in R

不想你离开。 提交于 2019-12-08 11:18:38

问题


Often times in research we have to do a summary table. I would like to create a table using tapply in R. The only problem is I have 40 variables and I would like to basically perform the same operation for all 40 variables. Here is an example of the data

Age Wt  Ht  Type
79  134 66  C
67  199 64  C
39  135 78  T
92  149 61  C
33  138 75  T
68  139 71  C
95  198 62  T
65  132 65  T
56  138 81  C
71  193 78  T

Essentially I would like to get it to produce the means of all the variables given the Type. It should look as

      C     T
Age 72.4   60.6
Wt  151.8  159.2
Ht  68.6   71.6

I tried using

sapply(df, tapply(df, df$Type, mean)) 

but got an error.

Any guidance would be appreciated.


回答1:


Try:

> sapply(df[1:3], tapply, df$Type, mean)
   Age    Wt   Ht
C 72.4 151.8 68.6
T 60.6 159.2 71.6

alternatively you can use colMeans:

> sapply(split(df[1:3], df$Type), colMeans)
        C     T
Age  72.4  60.6
Wt  151.8 159.2
Ht   68.6  71.6



回答2:


You could use aggregate :

res <- aggregate(DF[,names(DF) != 'Type'],list(DF$Type),mean)
> res
  Group.1  Age    Wt   Ht
1       C 72.4 151.8 68.6
2       T 60.6 159.2 71.6

then transposing it :

m <- t(res[-1]) # convert the data.frame (excluding first col) in a matrix and traspose it
colnames(m) <- res[[1]] # set colnames of the matrix taking them from the data.frame 1st col
> m
        C     T
Age  72.4  60.6
Wt  151.8 159.2
Ht   68.6  71.6


来源:https://stackoverflow.com/questions/37730342/using-variations-of-apply-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!