如何按组获取平均值? [英] How can I get mean values by group?
问题描述
我想按组高低分别获得var1和var2的平均值。
如何获得按组(低和高)分别表示的两个变量的平均值?
I want to get mean of var1 and var2 by group low and high. How can I get mean of two variables each by group (low and high) ?
ID var1 var2 low high
1 1 6 0 1
2 2 7 0 1
3 3 8 1 0
4 4 9 1 0
5 5 10 0 1
推荐答案
聚合
在给出正确输入的情况下满足您的需求。
aggregate
does what you need, given the proper input.
要获取多个列的聚合,您可以可以 cbind
使其成为结果中的单独列:
To get the aggregate of multiple columns, you can cbind
them so that they are separate columns in the result:
aggregate(cbind(var1, var2) ~ low+high, data=x, FUN=mean)
## low high var1 var2
## 1 1 0 3.500000 8.500000
## 2 0 1 2.666667 7.666667
如果要获取除以外的所有其他列的均值低
和高
,。
很方便,意思是所有其他列:
If you want to take the mean of every column other than low
and high
, .
is handy, meaning "all other columns":
aggregate(. ~ low+high, data=x, FUN=mean)
## low high ID var1 var2
## 1 1 0 3.500000 3.500000 8.500000
## 2 0 1 2.666667 2.666667 7.666667
请注意, +
在公式中具有特殊含义如果它在〜
的右侧。这并不意味着总和,而是意味着同时使用两个因素。在左侧,表示加法。
Note that +
has a special meaning in the formula if it is on the right side of the ~
. It doesn't mean a sum, but it means using both factors. On the left side, it means addition.
这篇关于如何按组获取平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!