Row wise sum – row sum in R dataframe

Row wise sum of the dataframe in R or sum of each row is calculated using rowSums() function. Other method to get the row sum in R is by using apply() function. row wise sum of the dataframe is also calculated using dplyr package. rowwise() function of dplyr package along with the sum function is used to calculate row wise sum. we will be looking at the following examples

  • Row wise sum in R dataframe using rowSums()
  • Row sum of the dataframe using apply() function.
  • Row wise sum of the dataframe using dplyr package.

row wise sum in R using rowSums() and sum() 21

First let’s create the dataframe

### Create dataframe 
df1 = data.frame( Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
 Mathematics1_score=c(62,47,55,74,32,77,86),
 Mathematics2_score=c(45,78,44,89,66,49,72),
 Science_score=c(56,52,45,88,33,90,47))

df1

df1 will be

Row wise sum in R dataframe Row sum in R 1

 

 

Row wise sum in R dataframe using rowSums()

Let’s calculate the row wise sum using rowSums() function as shown below. rowSums() function takes up column 2 to 4 and performs row wise sum

## row wise sum using rowSums()

df1$row_sum = rowSums(df1[,c(2,3,4)])
df1

or


## row wise sum using rowSums()

df1$row_sum = rowSums(df1[,c(-1)])
df1

so the resultant dataframe will be

Row wise sum – row sum in R dataframe 2

 

 

Row wise sum  in R dataframe using apply() function

Let’s calculate the row wise sum using apply() function as shown below. apply() function takes three arguments first argument is dataframe without first column and second argument is used to perform row wise operation (argument 1- row wise ; 2 – column wise ). third argument sum function sums up the values.  so here it performs row wise sum


### Row wise sum using apply() function

df1$row_sum = apply(df1[,-1], 1, sum)
df1

So the resultant dataframe with row wise sum calculated will be

Row wise sum – row sum in R dataframe 3

 

Row wise sum of the dataframe using dplyr: Method 1

rowSums() function takes up the columns 2 to 4 and performs the row wise operation with NA values replaced to zero.  row wise sum is performed using pipe (%>%) operator of the dplyr package.

##### Dplyr row wise sum
library(dplyr)

df1 %>% replace(is.na(.), 0) %>% mutate(row_wise_sum = rowSums(.[2:4]))

So the resultant dataframe with row wise sum calculated will be

Row wise sum in R using dplyr 1

 

Row wise sum of the dataframe using dplyr: Method 2

Row wise sum is calculated with the help rowwise() function of dplyr package  and sum() function as shown below

## row wise sum using dplyr
 
library(dplyr)

df1 %>%
  rowwise() %>%
  mutate(
    Total_price = sum(c(Mathematics1_score,Mathematics2_score,Science_score))
  )

row wise sum of “Mathematics1_score” , “Mathematics2_score” and “Science_score” is calculated and populated for each row as shown below

Row wise sum in R using dplyr 2

For more Details kindly refer to matrixStats package in R

 


Other Related Topics

 

                                                                                                           

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts