In the second slot, we first use the R library(dplyr), that has the option for bind_row that combines two data frame x & y with unequal column if there as ‘na’. using this colSums function from (‘dplyr’) helps to get sum & mean of columns of combined data frame z1, with ‘na’ ignored.
library(dplyr)
z1<-bind_rows(x,y, z)
colSums(z1, na.rm = TRUE, dims = 1)
## V1 V2 V3 V4 V5 v1 v2 v3
## 40.0 65.0 90.0 82.0 98.0 93.4 642.0 150.0
round(colMeans(z1, na.rm = TRUE, dims = 1), 1)
## V1 V2 V3 V4 V5 v1 v2 v3
## 5.7 9.3 12.9 20.5 24.5 10.4 71.3 16.7
This is asecond approch of the same problem where we have two data frame, and one data frame have more variable and more observarion. In the second slot, we first use the R library(dplyr), that has the option for bind_row that combines two data frame x & y with unequal column if there as ‘na’. using this colSums function from (‘dplyr’) helps to get sum of columns of combined data frame z, with ‘na’ ignored.first data is named tree that has 3 variables Girth, Height & Volume. Same variables are created with name df secondly having more records.
# make first data frame 'trees'
Girth <- c(8.3, 8.6, 10.5, 10.7, 11.2, 10.8, 11, 11.1, 11.2)
Height <- c(70, 65, 81, 65, 65, 66, 75, 80, 75)
Volume <- c(18.2, 10.3, 10.2, 16.4, 18.8, 19.7, 15.6, 18.2, 22.6)
trees <- data.frame(Girth, Height, Volume)
head(trees)
## Girth Height Volume
## 1 8.3 70 18.2
## 2 8.6 65 10.3
## 3 10.5 81 10.2
## 4 10.7 65 16.4
## 5 11.2 65 18.8
## 6 10.8 66 19.7
# Create 3 variables insecond data frame 'tree2'and combine
Girth <- c(8.8, 8.3, 8.6, 10.5, 10.7, 11.2, 10.8, 11, 11, 11.1, 11.2, 8.8)
Height <- c(70, 65, 72, 81, 65, 65, 83, 66, 75, 80, 75, 66)
Volume <- c(18.2, 18.2, 10.3, 10.3, 10.2, 16.4, 18.8, 19.7, 15.6, 18.2, 22.6, 19.9)
tree2 <- data.frame(Girth, Height, Volume)
tree2
## Girth Height Volume
## 1 8.8 70 18.2
## 2 8.3 65 18.2
## 3 8.6 72 10.3
## 4 10.5 81 10.3
## 5 10.7 65 10.2
## 6 11.2 65 16.4
## 7 10.8 83 18.8
## 8 11.0 66 19.7
## 9 11.0 75 15.6
## 10 11.1 80 18.2
## 11 11.2 75 22.6
## 12 8.8 66 19.9
Both data frame trees and trees2 are joined and sum function taken.
zz<-bind_rows(trees,tree2)
colSums(zz, na.rm = TRUE, dims = 1)
## Girth Height Volume
## 215.4 1505.0 348.4
colMeans(zz, na.rm = TRUE, dims = 1)
## Girth Height Volume
## 10.25714 71.66667 16.59048