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

3 datasets combine to 1 with conditions in R

I have two datasets

id <- c(1,2,3,4,5)
value1 <- c(20, 10, 15, 20, 30)
month <- c(2, 3, 4, 2, 3)
df1 <- dataframe(id,value1,month)

and the second dataset

id <- c(1,2,3,4,5)
value2 <- c(30, 25, 10, 30, 20)
month <- c(2, 3, 2, 2, 4)
df2 <- dataframe(id,value2,month)

The wanted output is a combined dataset(df3) with (id,value1,value2,month). However, only the id’s with matching months should be displayed so id 3 and 5 should not be displayed in df3

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 :

simple joins:

base

merge(df1, df2, by = c('id', 'month'))

dplyr

dplyr::inner_join(df1, df2, by = c('id', 'month'))
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