burton030
burton030

Reputation: 405

sum different columns in a data.frame

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!

Upvotes: 6

Views: 27138

Answers (3)

Ricardo Saporta
Ricardo Saporta

Reputation: 55420

If you dont want to include every single column, you somehow have to indicate which ones to include (or alternatively, which to exclude)

colsInclude <- c("X1961", "X1962", "X1963") # by name
    # or # 
colsInclude <- paste0("X", 1961:2003)  # by name
    # or # 
colsInclude <- c(10:19, 23, 55, 147)   # by column number

To put those columns in a new data frame simply use [ ] as you've done: '

newDF <- oldDF[, colsInclude]

To sum up each column, simply use colSums

sums <- colSums(newDF, na.rm=T)
    # or # 
sums <- colSums(oldDF[, colsInclude], na.rm=T)    

Note that sums will be a vector, not necessarilly a data frame. You can make it into a data frame using as.data.frame

sums <- as.data.frame(sums)
    # or, to include the data frame from which it came #
sums <- rbind(newDF, "totals"=sums)

Upvotes: 2

flodel
flodel

Reputation: 89097

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)

Upvotes: 14

Sven Hohenstein
Sven Hohenstein

Reputation: 81743

res <- unlist(lapply(production, function(x) if(is.numeric(x)) sum(x, na.rm=T)))

will return the sum of each numeric column.

You could create a new data frame based on the result with

data.frame(t(res))

Upvotes: 4

Related Questions