I have a dataframe, and I would like to check whether there are duplicated values for a specific column for each id. How can I identify duplicated rows, that are not only duplicated on id, but also the 'value' column?
df <- data.frame('id' = c('1','1', '2', '3', '3', '4','5','5','5'), "value" = c('apple','apple', 'orange', 'banana', 'banana', 'apple','orange','banana','orange'), "shop" = c('supermarket','café', 'café', 'supermarket', 'café', 'supermarket','supermarket','supermarket','café'))
My approach has been
#extract duplicates in the dataframe on value
df_dup <- df[duplicated(df$value), ]
#from this df, extract duplicates on id
df_dup1 <- df_dup[duplicated(df_dup$id), ]
However this method does not work. The output I am looking for is a reduced dataframe where only id's that have a duplicated value on the 'value'-column are kept together with the other variables in the dataframe:
df_exp <- data.frame('id' = c('1','1','3', '3', '5','5'), "value" = c('apple','apple', 'banana', 'banana', 'orange','orange'), "shop" = c('supermarket', 'café', 'supermarket', 'café' ,'supermarket','café'))
Thanks in advance!