0

I have two data frames, DF1 has many columns and tag column which has duplicate values:

... tag
... cat
... dog
... cat
... pencil
... ...

DF2 has two columns, tag and type, tag values are unique, type are not:

tag    type
car    vehicle
cat    animal  
dog    animal
pencil object
...    ...

It's not necessarily true that each tag from DF1 is described in DF2. I'd like to add column to DF1 which would contain type for each tag, for example:

... tag     type
... cat     animal
... dog     animal
... cat     animal
... pencil  object
... ...

Type for any tags from DF1 which are not in DF2 should be NA.

I made a loop to solve this, but it's very slow for a bigger data set. I assume that something like this can be done more elegant in R?

enedene
  • 3,525
  • 6
  • 34
  • 41

2 Answers2

0

I think this would work:

Total <- merge(DF1, DF2, by="type")

It will give you 3 columns. What you want is 1st and 3rd column. So remove the 2nd column.

user3077261
  • 133
  • 1
  • 8
  • this will not work - default of merge is inner join. `merge(DF1, DF2, by = "type", all.x = TRUE)` would work – Josh W. May 18 '15 at 18:30
0

This is doable with dplyr (among other methods). Try:

library("dplyr")

total <- DF1 %>% left_join(DF2)
Josh W.
  • 1,123
  • 1
  • 10
  • 17