Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum different columns in a data.frame

Tags:

dataframe

r

I have a very big data.frame and want to sum the values in every column.

So I used the following code:

sum(production[,4],na.rm=TRUE)

or

sum(production$X1961,na.rm=TRUE)

The problem is that the data.frame is very big. And I only want to sum 40 certain columns with different names of my data.frame. And I don't want to list every single column. Is there a smarter solution?

At the end I also want to store the sum of every column in a new data.frame.

Thanks in advance!

like image 338
burton030 Avatar asked Nov 12 '12 21:11

burton030


People also ask

How do I sum multiple columns in a data frame?

Use DataFrame. groupby(). sum() to group rows based on one or multiple columns and calculate sum agg function. groupby() function returns a DataFrameGroupBy object which contains an aggregate function sum() to calculate a sum of a given column for each group.

How do you add two columns in a data frame?

Combine Two Columns Using + Operator By use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.

How do you sum across 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 I add 3 columns in pandas?

Add multiple columns to a data frame using Dataframe. assign() method. Using DataFrame. assign() method, we can set column names as parameters and pass values as list to replace/create the columns.


1 Answers

Try this:

colSums(df[sapply(df, is.numeric)], na.rm = TRUE)

where sapply(df, is.numeric) is used to detect all the columns that are numeric.

If you just want to sum a few columns, then do:

colSums(df[c("X1961", "X1962", "X1999")], na.rm = TRUE)
like image 154
flodel Avatar answered Oct 02 '22 16:10

flodel