Sum across multiple columns with dplyr

R Programming

My question involves summing up values across multiple columns of a data frame and creating a new column corresponding to this summation using dplyr. The data entries in the columns are binary(0,1). I am thinking of a row-wise analog of the summarise_each or mutate_each function of dplyr. Below is a minimal example of the data frame:

library(dplyr)

df=data.frame(

  x1=c(1,0,0,NA,0,1,1,NA,0,1),

  x2=c(1,1,NA,1,1,0,NA,NA,0,1),

  x3=c(0,1,0,1,1,0,NA,NA,0,1),

  x4=c(1,0,NA,1,0,0,NA,0,0,1),

  x5=c(1,1,NA,1,1,1,NA,1,0,1))

 

> df

   x1 x2 x3 x4 x5

1   1  1  0  1  1

2   0  1  1  0  1

3   0 NA  0 NA NA

4  NA  1  1  1  1

5   0  1  1  0  1

6   1  0  0  0  1

7   1 NA NA NA NA

8  NA NA NA  0  1

9   0  0  0  0  0

10  1  1  1  1  1

I could use something like:

 df <- df %>% mutate(sumrow= x1 + x2 + x3 + x4 + x5)

but this would involve writing out the names of each of the columns. I have like 50 columns. In addition, the column names change at different iterations of the loop in which I want to implement this operation so I would like to try to avoid having to give any column names.

How can I do that most efficiently? Any assistance would be greatly appreciated.

1
Answers

Replies


You can do this in two ways. One option is to some down each column like below.



df %>%


replace(is.na(.), 0) %>%


select_if(is.numeric) %>%


summarise_all(funs(sum))



You can also sum up each row like below.



df %>%


replace(is.na(.), 0) %>%


mutate(sum = rowSums(.[1:5]))


 

 
 

If you want to unleash your potential in this competitive field, please visit the R Programming course page for more information, where you can find the R Programming tutorials and R Programming frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
To Top