简体   繁体   中英

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. How can I do that?

And 2nd question: How do I get the number of observations for every combination of L and M? 2 obs for A and A, 1 for A and B, 3 for A and 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 . to select all other columns as grouping except the one of the lhs ie "W"

aggregate(W ~., df1, mean)

or with 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

data

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM