Home > front end >  Group when values in two columns are identical and calculate the mean
Group when values in two columns are identical and calculate the mean

Time:12-01

I got a dataset like this:

df1 <- data.frame(
  var1 = c(1, 1, 1, 2), 
  var2 = c(1, 2, 2, 1),
  value = c(1, 2, 3, 4))

I want to group rows in var1 and var2 and calculate the mean of value, and the condition is that when rows with the same var1 and var2 values will be grouped together (so it is not simply grouped by unique values in var1 and var2).

The output dataset will be this:

df2 <- data.frame(
  var1 = c(1, 1, 2),
  var2 = c(1, 2, 1),
  value = c(1, 2.5, 4))

How can I do this?

CodePudding user response:

You may try

library(dplyr)
df1 %>%
  group_by(var1, var2) %>%
  summarise(value = mean(value))

   var1  var2 value
  <dbl> <dbl> <dbl>
1     1     1   1  
2     1     2   2.5
3     2     1   4  

group_by(var1, var2) will group both variable together.

CodePudding user response:

Using aggregate().

aggregate(value ~ var1   var2, df1, mean)
#   var1 var2 value
# 1    1    1   1.0
# 2    2    1   4.0
# 3    1    2   2.5
  •  Tags:  
  • r
  • Related