0

I have two different datasets of unequal sizes. In addition, one has collapsed subgroups. Here is the data:

df = data.frame(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 4L), 
            ID_name = c("AA", "AA", "AA", "BB", "BB", "BB", "DD", "DD", "DD"), 
            Volume = c(10L, 20L, 30L, 50L, 50L, 40L, 20L, 30L, 10L))

df1 = data.frame(ID = c(1L, 2L, 4L), 
                ID_name = c("AA", "BB","DD"), 
                Pop = c(2200L, 3300L, 2000L))

I want my results to look like this

dfmerged = data.frame(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 4L), 
                      ID_name = c("AA", "AA", "AA", "BB", "BB", "BB", "DD", "DD", "DD"), 
                      Volume = c(10L, 20L, 30L, 50L, 50L, 40L, 20L, 30L, 10L),
                      Pop = c(2200L, 2200L, 2200L, 3300L, 3300L, 3300L, 2000L, 2000L, 2000L))

Does dplyr has such function? Thanks

Felipe Alvarenga
  • 2,572
  • 1
  • 17
  • 36
T.Z
  • 65
  • 7
  • 1
    How about `left_join` i.e. `left_join(df, df1)` – akrun Dec 07 '17 at 18:38
  • 1
    Just like SQL's `join`, datasets do not need to be same length to merge. That is a `cbind` requirement. In fact your merge is one-to-many. Did you try `merge`? – Parfait Dec 07 '17 at 18:38
  • 1
    `merge(df, df1)` should work – markdly Dec 07 '17 at 18:40
  • 1
    `dfmerged1 = left_join(df, df1, by = c("ID", "ID_name"))` testing: `identical(dfmerged, dfmerged1) # TRUE` – Felipe Alvarenga Dec 07 '17 at 18:41
  • Thank you, all! Somehow when i tried on my dataset, it didn't seem to work. Now i tried on a test data and it worked. I spotted the mistake in my old code. Thanks all - you helped me tremendously! – T.Z Dec 07 '17 at 20:20

0 Answers0