Search code examples
rdatabasesumrow

Summing multiple columns across a single row, for multiple rows


I have a large dataframe [7872 x 7872], which essentially looks like this:

df <- data.frame(X0.61=c(1, 2, 3, 4, 5),
             X0.225=c(3, 4, 5, 6, 7),
             X0.329=c(4, 5, 6, 7, 8),
             X0.553=c(5, 6, 7, 8, 9))

I want to, for example, sum row 1 and 3 for columns X0.61, X0.225 and X0.553. What would be an efficient way to do this? I have to do these kinds of sums for 100s of rows and at the moment I am using the following tedious code:

x <- sum(EXIO[1, c(61, 225, 389, 553, 717, 881)])

EXIO being my data, 1 the first row and in c all the columns. There has to be a way more efficient way to do this for ~1600 rows right? I am still quite a R novice.

Eventually I need to combine all these sums in new rows and columns to combine them with another dataset. If any of you could provide a general code/package for me to fool around with at a later point this would also be greatly appreciated.


Solution

  • I'm not sure if this is part of a larger data frame and how you intend to apply this there, but I might use the map function in purrr.

    library(purrr)
    df <- data.frame(X0.61=c(1, 2, 3, 4, 5),
                 X0.225=c(3, 4, 5, 6, 7),
                 X0.329=c(4, 5, 6, 7, 8),
                 X0.553=c(5, 6, 7, 8, 9))
    map(df[c(1,3), c(1,2,4)], sum)
    
    
    $X0.61
    4
    $X0.225
    8
    $X0.553
    12