如何对data.frame列值求和? [英] How to sum data.frame column values?

查看:348
本文介绍了如何对data.frame列值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几列的数据框;一些数字和一些字符. 如何计算特定列的总和?我已经为此进行了搜索,并且看到了很多函数(sumcumsumrowsumrowSumscolSumsaggregateapply),但我无法理解所有内容.

I have a data frame with several columns; some numeric and some character. How to compute the sum of a specific column? I’ve googled for this and I see numerous functions (sum, cumsum, rowsum, rowSums, colSums, aggregate, apply) but I can’t make sense of it all.

例如,假设我有一个包含以下列的数据框people

For example suppose I have a data frame people with the following columns

people <- read(
  text = 
    "Name Height Weight
    Mary 65     110
    John 70     200
    Jane 64     115", 
  header = TRUE
)
…

如何获得所有权重的总和?

How do I get the sum of all the weights?

推荐答案

您可以只使用sum(people$Weight).

sum对向量求和,people$Weight从数据框中检索权重列.

sum sums up a vector, and people$Weight retrieves the weight column from your data frame.

注意-您可以使用?sum?colSums等获得内置帮助(顺便说一句,colSums将为您提供每一列的总和).

Note - you can get built-in help by using ?sum, ?colSums, etc. (by the way, colSums will give you the sum for each column).

这篇关于如何对data.frame列值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆