Row wise sum – row sum in R dataframe

Row wise sum of the dataframe in R is calculated using rowsums()  function.Lets see how to create a row sum in R

First lets create the 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 will be

Row wise sum in R dataframe Row sum in R 1

 

Row wise sum in R dataframe:

We compute row wise sum of mathematics1_score and mathematics2_score


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

so the resultant dataframe will be

Row wise sum in R dataframe Row sum in R 2

 

Now let’s sum up all the score and create the total score


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

so the resultant dataframe will be

Row wise sum in R dataframe Row sum in R 3

 

Row wise sum – row sum in R dataframe - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                                Row wise sum – row sum in R dataframe - image next_small-11-1 on http://www.datasciencemadesimple.com