0
votes

How can I sum one specific column in all data frames in a list an put them in a new data frame? An small example is:

A <- data.frame(matrix( nrow = 2, ncol = 2))
B <- data.frame(matrix( nrow = 2, ncol = 2))

A[,] <- 3
B[,] <- 4

l <- list(A,B)

So let's say I want to sum up all columns "X1" in my list and put in one data frame (vector, since there only should be one row). This data frame should then have value 6 (3+3) in first row and 8 (4+4) in the second.

In the real data I have 18 data frames in the list and the columns to sum in each data frame is of different lenght.

Mabye I should use the sapply or lapply function?

2

2 Answers

0
votes

You can use colSums, i.e.

do.call(rbind, lapply(l, function(i)colSums(i['X1'])))
#     X1
#[1,]  6
#[2,]  8
0
votes

Here is one option with sapply where we Extract the column 'X1' into a matrix and then do the colSums

colSums(sapply(l, `[[`, 'X1'))
#[1] 6 8

Or with map from purrr

library(purrr)
library(dplyr)
map_dbl(l, ~ .x %>% 
               pull(X1) %>% 
               sum)
#[1] 6 8

If it is needed as a data.frame

map_dfr(l, ~ .x %>% 
               summarise(X1 = sum(X1)))
#   X1
#1  6
#2  8