如何对 data.frame 列值求和?

我有一个包含几列的数据框架; 一些是数字,一些是字符。我为此在谷歌上搜索了很多函数(sumcumsumrowsumrowSumscolSumsaggregateapply) ,但是我不能完全理解它们。

例如,假设我有一个具有以下列的数据帧 people

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

我如何得到所有权重的总和?

517941 次浏览

You can just use sum(people$Weight).

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

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

When you have 'NA' values in the column, then

sum(as.numeric(JuneData1$Account.Balance), na.rm = TRUE)

to order after the colsum :

order(colSums(people),decreasing=TRUE)

if more than 20+ columns

order(colSums(people[,c(5:25)],decreasing=TRUE) ##in case of keeping the first 4 columns remaining.

To sum values in data.frame you first need to extract them as a vector.

There are several way to do it:

# $ operatior
x <- people$Weight
x
# [1] 65 70 64

Or using [, ] similar to matrix:

x <- people[, 'Weight']
x
# [1] 65 70 64

Once you have the vector you can use any vector-to-scalar function to aggregate the result:

sum(people[, 'Weight'])
# [1] 199

If you have NA values in your data, you should specify na.rm parameter:

sum(people[, 'Weight'], na.rm = TRUE)

you can use tidyverse package to solve it and it would look like the following (which is more readable for me):

library(tidyverse) people %>% summarise(sum(weight))