[英]Calculate the mean of a column by all combinations of the 2 other columns in R
I want to calculate the mean for every possible combination of 1st and 2nd column.我想计算第一列和第二列的每个可能组合的平均值。 So for the rows 1-2, 3, 4-6, and so on.
所以对于第 1-2、3、4-6 行,依此类推。 How can I do that?
我怎样才能做到这一点?
And 2nd question: How do I get the number of observations for every combination of L and M?第二个问题:如何获得 L 和 M 的每种组合的观察次数? 2 obs for A and A, 1 for A and B, 3 for A and I,...
2 obs 用于 A 和 A,1 用于 A 和 B,3 用于 A 和 I,...
L M W
1 A A 61.5
2 A A 68.2
3 A B 64.0
4 A I 65.0
5 A I 59.7
6 A I 55.0
7 B A 42.0
8 B A 60.2
9 B B 52.5
10 B I 61.8
11 B I 49.5
12 B I 52.7
We can use aggregate
from base R
with a formula method and specify the .
我们可以通过公式方法使用来自
base R
的aggregate
并指定.
to select all other columns as grouping except the one of the lhs
ie "W"到 select 所有其他列作为分组,除了
lhs
之一,即“W”
aggregate(W ~., df1, mean)
or with dplyr
或与
dplyr
library(dplyr)
df1 %>%
group_by(across(where(is.character))) %>%
summarise(Mean = mean(W), Count = n())
-output -输出
# A tibble: 6 x 4
# Groups: L [2]
L M Mean Count
<chr> <chr> <dbl> <int>
1 A A 64.8 2
2 A B 64 1
3 A I 59.9 3
4 B A 51.1 2
5 B B 52.5 1
6 B I 54.7 3
df1 <- structure(list(L = c("A", "A", "A", "A", "A", "A", "B", "B",
"B", "B", "B", "B"), M = c("A", "A", "B", "I", "I", "I", "A",
"A", "B", "I", "I", "I"), W = c(61.5, 68.2, 64, 65, 59.7, 55,
42, 60.2, 52.5, 61.8, 49.5, 52.7)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
Using dplyr
:使用
dplyr
:
library(dplyr)
df %>%
group_by(L, M) %>%
summarise(mean=mean(W),
count=n())
returns返回
# A tibble: 6 x 4
# Groups: L [2]
L M mean count
<chr> <chr> <dbl> <int>
1 A A 64.8 2
2 A B 64 1
3 A I 59.9 3
4 B A 51.1 2
5 B B 52.5 1
6 B I 54.7 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.