Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging two dataframes in R with missing values

I have two dataframes that look like this:

df_1 <- tibble(id = c(1,1,2,2,3,3), y = c("a", "b", "a", "b","a", "b"))
df_2 <- tibble(id = c(1,1,3,3), z = c(4,5,6,5))

I want to merge the two dfs such that it looks like this:

df_3 <- tibble(id = c(1,1,2,2,3,3), y = c("a", "b", "a", "b","a", "b"), z = c(4,5,NA,NA,6,5))

How may I do this in R? Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Create a sequence column by ‘id’ and then join (as there are duplicates for ‘id’ in both datasets)

library(dplyr)
library(data.table)
df_1 %>% 
   mutate(rn = rowid(id)) %>%
   left_join(df_2 %>% 
      mutate(rn = rowid(id))) %>% 
    select(-rn)

-output

# A tibble: 6 × 3
     id y         z
  <dbl> <chr> <dbl>
1     1 a         4
2     1 b         5
3     2 a        NA
4     2 b        NA
5     3 a         6
6     3 b         5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading