akash87
akash87

Reputation: 3994

Using variations of `apply` in R

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.

Upvotes: 0

Views: 142

Answers (2)

digEmAll
digEmAll

Reputation: 57210

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

Upvotes: 1

Ernest A
Ernest A

Reputation: 7839

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

Upvotes: 2

Related Questions