0

I have a data.frame and want to create a new column (varNEW) based on matching values in all rows of two existing columns (var1 and var2) using R. The new column should be the SUM of the matched column rows' values in a third column (var3). I have provided an example of the data.frame and the desired results below:

var1  var2  var3  varNew
a     e     10    21    
a     e     11    21
b     e     5     5
b     f     1     1
c     e     6     26
c     e     20    26
d     e     10    10
d     f     9     9

Where do I start?

Dorothy
  • 523
  • 1
  • 5
  • 17
  • 1
    This is certainly a duplicate. Here's a base solution ```ave(DF[['var3']], DF[, c('var1', 'var2')], FUN = sum)```. – Cole Jul 27 '19 at 19:05
  • Thanks! I also used 'paste' to create a new 'group' column which merges var1 & var2 values (i.e., df$group <- paste(df$var1, df$var2), then used 'ave' – Dorothy Jul 27 '19 at 20:00

0 Answers0