Merging 2 variables in common and uncommon

Hei,
Can anyone suggest me a solution in these 2 assuming datasets
data1 :ID<-c[1 2 3 4 5 6 ]
data2:ID<-c[1 2 3 4 5 7 8 9]
I want to merge both ID variables in common and gets the result as intersection(common)i-e
ID:[1 2 3 4 5]and identify these values which is not in data1.Which is exactly here is 6.i need to find this value 6 but how can i do it in R?

dat1 <- c(1,2,3,4,5,6)
dat2 <- c(1,2,3,4,5,7,8,9)

dat2 %in% dat1

It would help to have a n actual data set.

1 Like

Try this:

library("tidyverse")

my_data_1 <- tibble(
  ID = c(1,2,3,4,5,6),
  v1 = runif(length(ID))
)

my_data_2 <- tibble(
  ID = c(1,2,3,4,5,7,8,9),
  v2 = runif(length(ID))
)

my_data_1 %>% 
  inner_join(my_data_2,
             by = "ID")

Hope it helps :slightly_smiling_face:

2 Likes

Uncommon<-anti_join(data1,data2,by=“ID”)
So i can find value 6
Common<-merge(data1,data2,by=“ID”)
So i can find values (1,2,3,4,5)which are common.
Maybe it helps others too :slight_smile: