Sum all values in every column of a data.frame in R

R

R Problem Overview


Given this data set:

  Name Height Weight
1 Mary     65    110
2 John     70    200
3 Jane     64    115

I'd like to sum every qualifier columns (Height and Weight) yielding

 199  425

The problem is that the qualifiers can be more than just 2 (i.e. more than just Height and Weight).

I can do this.

    # Create the dataframe people
    Name <- c("Mary", "John", "Jane")
    Height <- c(65,70,64)
    Weight <- c(110,200,115)
    people <- data.frame(Name, Height, Weight)

    res <- c(sum(people$Height),sum(people$Weight))
        

But it gets too long when the qualifier increase. What's the compact way to do it?

R Solutions


Solution 1 - R

You can use function colSums() to calculate sum of all values. [,-1] ensures that first column with names of people is excluded.

 colSums(people[,-1])
Height Weight 
   199    425

Assuming there could be multiple columns that are not numeric, or that your column order is not fixed, a more general approach would be:

colSums(Filter(is.numeric, people))

Solution 2 - R

We can use dplyr to select only numeric columns and purr to get sum for all columns. (can be used to get what ever value for all columns, such as mean, min, max, etc. )

library("dplyr")
library("purrr")

people %>%
    select_if(is.numeric) %>%
    map_dbl(sum)

Or another easy way by only using dplyr - As of (dplyr 1.0.0) we can use across()

library("dplyr")
people %>%
    summarise(across(where(is.numeric), ~ sum(.x, na.rm = TRUE)))

library("dplyr")
people %>%
    summarize_if(is.numeric, sum, na.rm=TRUE)

Solution 3 - R

mapply(sum,people[,-1])

Height Weight 
   199    425 

Solution 4 - R

For the sake of completion:

 apply(people[,-1], 2, function(x) sum(x))
#Height Weight 
#   199    425 

Solution 5 - R

you can also try a <- apply(mtcars[,c(select.colums)], 2, sum )

a <- apply(people[,c(2,3)], 2, sum )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpduboisView Question on Stackoverflow
Solution 1 - RDidzis ElfertsView Answer on Stackoverflow
Solution 2 - RcamnesiaView Answer on Stackoverflow
Solution 3 - RdondapatiView Answer on Stackoverflow
Solution 4 - RWorkhorseView Answer on Stackoverflow
Solution 5 - RSeyma KalayView Answer on Stackoverflow