How to Join Multiple Data Frames in R
How to Join Multiple Data Frames in R?, you can find it useful to connect many data frames in R. Fortunately, the left join() function from the dplyr package makes this simple to accomplish.
Crosstab calculation in R – Data Science Tutorials
library(dplyr)
Consider the following three data frames, for instance:
Let’s create a data frame
df1 <- data.frame(Q1 = c('a', 'b', 'c', 'd', 'e', 'f'), Q2 = c(152, 514, 114, 218, 322, 323))
df2 <- data.frame(Q1 = c('a', 'a', 'a', 'b', 'b', 'b'), Q3 = c(523, 324, 233, 134, 237, 141))
df3 <- data.frame(Q1 = c('P1', 'e', 'P2', 'g', 'P5', 'i'), Q4 = c(323, 224, 333, 324, 237, 441))
We can easily conduct two left joins, one after the other, to combine all three data frames.
Statistical test assumptions and requirements – Data Science Tutorials
connect the three data frames.
df1 %>% left_join(df2 , by='Q1') %>% left_join(df3, by='Q1')
Q1 Q2 Q3 Q4 1 a 152 523 NA 2 a 152 324 NA 3 a 152 233 NA 4 b 514 134 NA 5 b 514 237 NA 6 b 514 141 NA 7 c 114 NA NA 8 d 218 NA NA 9 e 322 NA 224 10 f 323 NA NA
Notably, the outcome of this join can also be saved as a data frame.
How to Count Distinct Values in R – Data Science Tutorials
After joining the three data frames, create an extra data frame called alldata and save the outcome.
alldata <- df1 %>% left_join(df2, by='Q1') %>% left_join(df3, by='Q1')
display the resultant data frame’s summary
glimpse(alldata)
Rows: 10 Columns: 4 $ Q1 <chr> "a", "a", "a", "b", "b", "b", "c", "d", "e", "f" $ Q2 <dbl> 152, 152, 152, 514, 514, 514, 114, 218, 322, 323 $ Q3 <dbl> 523, 324, 233, 134, 237, 141, NA, NA, NA, NA $ Q4 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, 224, NA