Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

aggregate - Get the means of sub groups of means in R

I'm a newbie of R and I don't know how to get R calculate the means of a subgroups of means which are the means of a subgroup themselves. I'll explain clearer.

I have a data frame like this:

GROUP WORD WLN
1     1    4
1     1    3
1     1    3
1     2    2
1     2    2
1     2    3
2     3    1
2     3    1
2     3    2
2     4    1
2     4    1
2     4    1
...   ...  ...

but the real one has a total of 5 groups and 25 words (5 words each group; every word has being assigned a number from 1 to 4 by 5 subjects...).

I need to get the means of WLN for every word and I can do that easily with a loop and save the results in a vector; but then I need a vector with the means of these means according to the group which the words belong to... So I need the means of means of words of the group 1, then of group 2, etc... (I don't know if I'm making it clear).

How can I get this without doing it one group by one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With base, using aggregate

> aggregate(WLN~GROUP+WORD, mean, data=df)
  GROUP WORD      WLN
1     1    1 3.333333
2     1    2 2.333333
3     2    3 1.333333
4     2    4 1.000000

where df is @Metrics' data.

Another alternative is using summaryBy from doBy package

> library(doBy)
> summaryBy(WLN~GROUP+WORD, FUN=mean, data=df)
  GROUP WORD WLN.mean
1     1    1 3.333333
2     1    2 2.333333
3     2    3 1.333333
4     2    4 1.000000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...