Search code examples
rdataframedata-manipulation

Calculation of a new variable in R


this is the dataframe

name  team   stat1    stat2    
a      aa        1          4              
b      aa        2          3
c      bb        3          2
d      bb        4          1

want to calculate a new variable which is calculated

-> (( stat1 of player 'a' / sum of stat1 for that team ) + ( stat2 of player 'a' / sum of stat2 for that team ))

-> ((1/1+2) + (4/4+3))

any idea on how to do this?


Solution

  • We can group by 'team', and then do the calculation to create a new column

    library(dplyr)
     df1 <- df1 %>%
        group_by(team) %>% 
        mutate(new = (stat1/sum(stat1) + (stat2/sum(stat2)))) %>%
       ungroup
    

    -output

    df1
    # A tibble: 4 × 5
      name  team  stat1 stat2   new
      <chr> <chr> <int> <int> <dbl>
    1 a     aa        1     4 0.905
    2 b     aa        2     3 1.10 
    3 c     bb        3     2 1.10 
    4 d     bb        4     1 0.905
    

    data

    df1 <- structure(list(name = c("a", "b", "c", "d"), team = c("aa", "aa", 
    "bb", "bb"), stat1 = 1:4, stat2 = 4:1), class = "data.frame", 
    row.names = c(NA, 
    -4L))