Sum of columns from different data frames (different lengths) in R
I am new to R and I have to do something as: I have to get a final result of the following: final_x=df1+df2+df3 where df1, df2, and df3 are three data frames with 1 column each (but different lengths) as follows:
df1
x1
1. 1
2. 4
3. 4
4. 6
df2
x2
1. 1
2. 4
3. 4
4. 6
5. 6
6. 6
7. 8
df3
x3
1. 1
2. 4
3. 4
4. 6
5. 6
As a final result, I would like my final_x to be the following:
final_x
final
1. 3
2. 12
3. 12
4. 18
5. 12
6. 6
7. 8
So to get the sum of each element from the columns, despite their different lengths. Basically, when I try to sum them up, I get the error saying '+' only defined for equally-sized data frames.
Here it is shown how we could do it:
#data:
df1 <- tibble(x1 = c(1,4,4,6))
df2 <- tibble(x2 = c(1,4,4,6,6,6,8))
df3 <- tibble(x3 = c(1,4,4,6,6))
# 1. construct a list
df_list <- list(df1, df2, df3)
#install.packages("qpcR")
library(qpcR)
# 2. Use `cbind.na` from gpcR package to fill the lacking length with `NA`, so all columns have the same length:
df_result <- do.call(qpcR:::cbind.na, df_list)
# 3. Use `rowSums` to sum
df_result$final <- rowSums(df_result, na.rm = TRUE)
# 4. save as dataframe
final_x <- data.frame(final = df_result[,4])
# 5. call result:
final_x
final
1 3
2 12
3 12
4 18
5 12
6 6
7 8
Here is another solution based on an old discussion from R-help at r-project.org.
This makes use of the custom base-R functions cbind.na()
and rbind.na()
from Andrej-Nikolai Spiess.
source("http://www.dr-spiess.de/scripts/cbind.na.R")
source("http://www.dr-spiess.de/scripts/rbind.na.R")
rowSums(cbind.na(df1, df2, df3), na.rm = TRUE)
#[1] 3 12 12 18 12 6 8