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.
For example suppose I have a data frame people with the following columns
people <- read.table(
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?
You can just use
sum(people$Weight).sumsums up a vector, andpeople$Weightretrieves the weight column from your data frame.Note – you can get built-in help by using
?sum,?colSums, etc. (by the way,colSumswill give you the sum for each column).