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

The Solution to Sum all values in every column of a data.frame in R is


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))

~ Answered on 2013-08-23 06:54:24


Most Viewed Questions: