Search code examples
rsum

Sum with 2 different condition


I have always use VBA for doing this operation but my commit prefer R. I need to sum column (c) if a and b has the same value. It is possible do it in R? the firt dataframe is my original, the second is my result. I can use group also if I use two condition? Important: name a and b sometimes can be the same.

a <- c("a1", "a1","a2","a2","a2","a2","a2","a2","a3")
b <- c("b1", "b1","b1","b2","b2","b3","b3","b4","b4")
c <- as.numeric(c("1500", "5000","3000","4600","4500","1000", "5000","8000","4000"))
df <- data.frame(a, b, c)

a1 <- c("a1","a2","a2","a2","a2","a3")
b1 <- c("b1","b1","b2","b3","b4","b4")
c1 <- as.numeric(c("6500","3000","9100","6000","8000","4000"))

result <- data.frame(a1, b1, c1)

Solution

  • Using dplyr:

    library(dplyr)
    
    a <- c("a1", "a1","a2","a2","a2","a2","a2","a2","a3")
    b <- c("b1", "b1","b1","b2","b2","b3","b3","b4","b4")
    d <- as.numeric(c("1500", "5000","3000","4600","4500","1000", "5000","8000","4000"))
    my_dataframe <- data.frame(a, b, d)
    
    my_dataframe %>% 
      group_by(a,b) %>% 
      summarise(d = sum(d)) %>% 
      ungroup()
    

    Result:

    # A tibble: 6 × 3
      a     b         d
      <chr> <chr> <dbl>
    1 a1    b1     6500
    2 a2    b1     3000
    3 a2    b2     9100
    4 a2    b3     6000
    5 a2    b4     8000
    6 a3    b4     4000