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

前端 未结 4 2134
暖寄归人
暖寄归人 2020-12-01 10:23

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 c

4条回答
  •  醉酒成梦
    2020-12-01 10:48

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

提交回复
热议问题