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