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