I have two dataframes that look like this:
df_1 <- tibble(id = c(1,1,1,2,2,2,3,3,3), y = c("a", "b", "c", "a", "b","c", "a", "b", "c"))
df_2 <- tibble(id = c(1,3), z = c(4,6))
I want to merge the two dfs such that it looks like this:
df_3 <- tibble(id = c(1,1,1,2,2,2,3,3,3), y = c("a", "b", "c", "a", "b","c", "a", "b", "c"), z = c(4,4,4,NA,NA,NA,6,6,6))
How will you do so in R? Thank you!
CodePudding user response:
left_join(df_1,df_2)
Output:
id y z
<dbl> <chr> <dbl>
1 1 a 4
2 1 b 4
3 1 c 4
4 2 a NA
5 2 b NA
6 2 c NA
7 3 a 6
8 3 b 6
9 3 c 6