Search code examples
rdataframedplyrcase-when

Numerical calculation using case_when


I have the following type of data df <- data.frame(Group=rep(c("A", "B"), 3), var1 = rnorm(6)*100)

I want to create a new variable by dividing var1 with a number specific for each group.

I tried, df %>% mutate(var2 = case_when(Group == "A" ~ var1/100, Group == "B" ~ var1/120)) Any suggestion to work with numerical calculations using case_when or any alternative apply family of functions?


Solution

  • If your data is large I would suggest that you create a mapping table which contains the factor for each group:

    mapping_factor <- tibble(Group = c("A", "B"), f = c(100, 120))
    

    Then you can simply join the mapping table and divide afterwards:

    df %>% left_join(mapping_factor, by = "Group") %>% 
      mutate(var2 = var1 / f)
    

    If your data is not that big nad you do not have many different Group attributes you can work with case_when:

    df %>% mutate(var2 = var1 / case_when(Group == "A" ~ 100, 
                                          Group == "B" ~  120))