6

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!

3 Answers 3

14

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)
1
  • 3
    This answer is better than the currently accepted answer because it makes explicit what is being summed (columns or rows) by using colsums. The other answer requires that the user know that sum works by column by default.
    – s_a
    Oct 21, 2014 at 14:04
4
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))
1
  • I know. But the problem is that there are also 40 comuns which are not numeric.
    – burton030
    Nov 12, 2012 at 22:04
2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.