Francesco Francesco - 2 months ago 10
R Question

Sum columns of every data.frame in a list, give an only data frame with the sums

I have a list of 17 data.frames, every one represents some variables of an economy in a year, from 1995 to 2011.

I would like to obtain the sum of every column of every database, and making a new database out of it. So the new database would be made of 17 rows and the same number of columns of the dataframes I have now, every row representing a year and every cell the sum of the variable in the column for that year.

I tried to write a function but really can't obtain what I want.

I know I have to use:

newdf <- lapply(list, FUN = functionname)


but I can't find how to write the function.

An example:

df1 <- data.frame(1:3,4:6)
colnames(df1) <- c("one", "two")
df2 <- data.frame(2:4, 3:5)
colnames(df2) <- c("one", "two")


I would like to obtain a new df:

df3 <- data.frame(c(6,9),c(15, 12))
colnames(df3) <- c("one","two")

one two
1 6 15
2 9 12

Answer

Using lapply :

df1 <- data.frame(1:3,4:6)
colnames(df1) <- c("one", "two")
df2 <- data.frame(2:4, 3:5)
colnames(df2) <- c("one", "two")

l <- list(df1,df2)

do.call(rbind,lapply(l, colSums))

EDIT : Try this one instead of the last line?

do.call(rbind,lapply(l, function(x) {
  colSums(x[,sapply(x, is.numeric)])
}))

EDIT 2:

df1 <- data.frame(1:3,4:6,c("a","asdf","asdf"))
colnames(df1) <- c("one", "two", "thr")
df2 <- data.frame(2:4, 3:5, c("asdf","casd","sdfasdf"))
colnames(df2) <- c("one", "two", "thr")

l <- list(df1,df2)

do.call(rbind,lapply(l, function(x) {
  colSums(x[,sapply(x, is.numeric)])
}))
Comments