Ryo
Ryo

Reputation: 167

R. How to add sum row in data frame

I know this question is very elementary, but I'm having a trouble adding an extra row to show summary of the row.

Let's say I'm creating a data.frame using the code below:

name <- c("James","Kyle","Chris","Mike")
nationality <- c("American","British","American","Japanese")
income <- c(5000,4000,4500,3000)
x <- data.frame(name,nationality,income)

The code above creates the data.frame below:

  name nationality income
1 James    American  5000
2  Kyle     British  4000
3 Chris    American  4500
4  Mike    Japanese  3000

What I'm trying to do is to add a 5th row and contains: name = "total", nationality = "NA", age = total of all rows. My desired output looks like this:

  name nationality  income
1 James    American  5000
2 Kyle     British   4000
3 Chris    American  4500
4 Mike    Japanese   3000
5 Total      NA      16500

In a real case, my data.frame has more than a thousand rows, and I need efficient way to add the total row.

Can some one please advice? Thank you very much!

Upvotes: 0

Views: 10524

Answers (3)

Ven Yao
Ven Yao

Reputation: 3710

using index.

name <- c("James","Kyle","Chris","Mike")
nationality <- c("American","British","American","Japanese")
income <- c(5000,4000,4500,3000)
x <- data.frame(name,nationality,income, stringsAsFactors=FALSE)

x[nrow(x)+1, ] <- c('Total', NA, sum(x$income))

UPDATE: using list

x[nrow(x)+1, ] <- list('Total', NA, sum(x$income))
x
#    name nationality income
# 1 James    American   5000
# 2  Kyle     British   4000
# 3 Chris    American   4500
# 4  Mike    Japanese   3000
# 5 Total        <NA>  16500
sapply(x, class)
#        name nationality      income 
# "character" "character"   "numeric" 

Upvotes: 1

StayLearning
StayLearning

Reputation: 691

If you want the exact row as you put in your post, then the following should work:

newdata = rbind(x, data.frame(name='Total', nationality='NA', income = sum(x$income)))

I though agree with Jaap that you may not want this row to add to the end. In case you need to load the data and use it for other analysis, this will add to unnecessary trouble. However, you may also use the following code to remove the added row before other analysis:

newdata = newdata[-newdata$name=='Total',]

Upvotes: -4

akrun
akrun

Reputation: 886938

We can use rbind

 rbind(x, data.frame(name='Total', nationality=NA, income = sum(x$income)))
 #    name nationality income
 #1 James    American   5000
 #2  Kyle     British   4000
 #3 Chris    American   4500
 #4  Mike    Japanese   3000
 #5 Total        <NA>  16500

Upvotes: 6

Related Questions