Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum data.frame column values?

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(
  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?

like image 597
User Avatar asked Mar 12 '12 23:03

User


People also ask

How do you sum a column value in Python?

To get the total or sum of a column use sum() method, and to add the result of the sum as a row to the DataFrame use loc[] , at[] , append() and pandas. Series() methods.

How do you sum up rows in a data frame?

To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.

How do you find the sum of two columns in a data frame?

Sum of two columns The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using “+” operator.


3 Answers

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

like image 145
mathematical.coffee Avatar answered Oct 10 '22 15:10

mathematical.coffee


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)
like image 34
Bulat Avatar answered Oct 10 '22 13:10

Bulat


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

sum(as.numeric(JuneData1$Account.Balance), na.rm = TRUE)
like image 2
Dheeraj Avatar answered Oct 10 '22 15:10

Dheeraj