Search code examples
rrowsum

Simplify multiple rowSums looping through columns


I'm currently on R trying to create for a DF multiple columns with the sum of previous one. Imagine I got a DF like this:

df=    
   sep-2016  oct-2016    nov-2016  dec-2016   jan-2017
1  70        153            NA        28        19
2  57         68            73       118        16
3  29         NA            19        32        36
4 177         36             3        54        53

and I want to add at the end the sum of the rows previous of the month that I'm reporting so for October you end up with the sum of sep and oct, and for November you end up with the sum of sep, oct and november and end up with something like this:

 df=    
     sep-2016  oct-2016    nov-2016  dec-2016   jan-2017 status-Oct2016 status-Nov 2016
    1  70        153            NA        28        19      223       223
    2  57         68            73       118        16      105       198
    3  29         NA            19        32        36       29        48
    4 177         36             3        54        53      213        93

I want to know a efficient way insted of writing a lots of lines of rowSums() and even if I can get the label on the iteration for each month would be amazing!

Thanks!


Solution

  • We can use lapply to loop through the columns to apply the rowSums.

    dat2 <- as.data.frame(lapply(2:ncol(dat), function(i){
      rowSums(dat[, 1:i], na.rm = TRUE)
    }))
    
    names(dat2) <- paste0("status-", names(dat[, -1]))
    
    dat3 <- cbind(dat, dat2)
    
    dat3
    #   sep-2016 oct-2016 nov-2016 dec-2016 jan-2017 status-oct-2016 status-nov-2016 status-dec-2016 status-jan-2017
    # 1       70      153       NA       28       19             223             223             251             270
    # 2       57       68       73      118       16             125             198             316             332
    # 3       29       NA       19       32       36              29              48              80             116
    # 4      177       36        3       54       53             213             216             270             323
    

    DATA

    dat <- read.table(text = "   'sep-2016'  'oct-2016'    'nov-2016'  'dec-2016'   'jan-2017'
    1  70        153            NA        28        19
                      2  57         68            73       118        16
                      3  29         NA            19        32        36
                      4 177         36             3        54        53",
                      header = TRUE, stringsAsFactors = FALSE)
    
    names(dat) <- c("sep-2016", "oct-2016", "nov-2016", "dec-2016", "jan-2017")