1

I want to merge 6 datasets that have an ID variable. I want to have one data set with ID values common to all datasets.

I know this is an easy fix, but I haven;t come across a help topic

ex.

id month sbp dpb
D1  3     40  40 
D1  4     10  10
D1  3     20  20
D2  4     30  20
D3  5     10  40
D1  3     40  40

id month sbp dpb
D1  3     40  40 
D1  4     10  10
D2  3     20  20
D4  4     30  20
D3  5     10  40
D1  3     40  40

final

id month sbp dpb
D1  3     40  40 
D1  4     10  10
D1  3     20  20
D2  4     30  20
D3  5     10  40
D1  3     40  40
D1  3     40  40 
D1  4     10  10
D2  3     20  20
D3  5     10  40
D1  3     40  40

D4 is omitted from final dataset

2 Answers2

0

As we have 6 datasets (assuming that the objects are 'df1', 'df2',... 'df6'), get the values of them in a list with mget, then bind them together (bind_rows) and filter out the 'id's that are not common in all of them

library(dplyr)
n <- 2 #Based on the example only two objects, change it to 6
mget(paste0("df", seq_len(n))) %>%
          bind_rows(., .id = 'grp') %>% 
          group_by(id) %>% 
          filter(n_distinct(grp)==n) %>%
          ungroup %>%
          select(-grp)
# A tibble: 11 x 4
#   id    month   sbp   dpb
#   <chr> <int> <int> <int>
# 1 D1        3    40    40
# 2 D1        4    10    10
# 3 D1        3    20    20
# 4 D2        4    30    20
# 5 D3        5    10    40
# 6 D1        3    40    40
# 7 D1        3    40    40
# 8 D1        4    10    10
# 9 D2        3    20    20
#10 D3        5    10    40
#11 D1        3    40    40

A base R option would be to get the 'id's that are common in all of the datasets with intersect

lst <- setNames(mget(paste0("df", seq_len(n))), NULL)
ids <- Reduce(intersect, lapply(lst, `[[`, 'id'))    
res <- do.call(rbind, lapply(lst, subset, subset = id %in% ids))
row.names(res) <- NULL
res
#   id month sbp dpb
#1  D1     3  40  40
#2  D1     4  10  10
#3  D1     3  20  20
#4  D2     4  30  20
#5  D3     5  10  40
#6  D1     3  40  40
#7  D1     3  40  40
#8  D1     4  10  10
#9  D2     3  20  20
#10 D3     5  10  40
#11 D1     3  40  40
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Is that what you are looking for? See code below:

df3 <- subset(df2, df2$id %in% df1$id)      
df <- rbind(df2, df3)
M_M
  • 899
  • 8
  • 21