How to sum data.frame column values?
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).
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)
When you have 'NA' values in the column, then
sum(as.numeric(JuneData1$Account.Balance), na.rm = TRUE)