Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add row to a data frame with total sum for each column

Tags:

dataframe

r

I have a data frame where I would like to add an additional row that totals up the values for each column. For example, Let's say I have this data:

x <- data.frame(Language=c("C++", "Java", "Python"),                  Files=c(4009, 210, 35),                  LOC=c(15328,876, 200),                  stringsAsFactors=FALSE)     

Data looks like this:

  Language Files   LOC 1      C++  4009 15328 2     Java   210   876 3   Python    35   200 

My instinct is to do this:

y <- rbind(x, c("Total", colSums(x[,2:3]))) 

And this works, it computes the totals:

> y   Language Files   LOC 1      C++  4009 15328 2     Java   210   876 3   Python    35   200 4    Total  4254 16404 

The problem is that the Files and LOC columns have all been converted to strings:

> y$LOC [1] "15328" "876"   "200"   "16404" 

I understand that this is happening because I created a vector c("Total", colSums(x[,2:3]) with inputs that are both numbers and strings, and it's converting all the elements to a common type so that all of the vector elements are the same. Then the same thing happens to the Files and LOC columns.

What's a better way to do this?

like image 755
Lorin Hochstein Avatar asked Feb 09 '11 15:02

Lorin Hochstein


People also ask

How do you sum up a row in Python?

To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.

How do I sum across rows of a DataFrame in R?

Syntax: mutate(new-col-name = rowSums(.)) The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame. Syntax: rowSums(.)

How do you sum certain rows?

1. Select a blank cell and enter formula =SUM(INDEX(Score_range,,2)) into the Formula Bar and then press the Enter key to get the result.


1 Answers

See adorn_totals() from the janitor package:

library(janitor) x %>%   adorn_totals("row")  #>  Language Files   LOC #>       C++  4009 15328 #>      Java   210   876 #>    Python    35   200 #>     Total  4254 16404 

The numeric columns remain of class numeric.

Disclaimer: I created this package, including adorn_totals() which is made for precisely this task.

like image 97
Sam Firke Avatar answered Oct 17 '22 20:10

Sam Firke