Merge ID with food but only matching values

Hi there,
I want to merge two dataframes in R studio but only by matching values. The 1st df have 1848 observations, while the other one have 1912 observations. See attatched file for the dataframes. The problem is that when I run my code:

mergetotal <- merge(mhdds55unik1, mhdds55unik3, by=c("hhid","Fødevare"), all=FALSE)

...it gives me 1912 observations, while I only want the 1848 observations.
Rkoder.pdf (165.7 KB)

What am i doing wrong?

Dplyr’s inner_join() does what you want: Join two tbls together — join • dplyr

test for uniqueness of the ids in both frames, you might find the number are elevated due to duplicates

Thank you - there was 44 duplicated. I used this code to see which ones:

mhdds55fdvid1 <- merge(mhdds55fdv, IDIC1111, by="hhid", all=FALSE)
mhdds55fdvid2 <- mhdds55fdvid1 %>% distinct(hhid, .keep_all = TRUE)

How do I extract mhdds55fdvid2 from mhdds55fdvid1?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.