0

I have a tibble where one column like this:

ID 
AA10
AA10
AB11
CC09
CC09  

I also have a tibble which is a sort of 'dictionary', with 2 columns. It is used to map 'ID' to 'animal':

ID     animal
AA10   dog
AB11   cat
CC09   lion

I now want to add a column to the first tibble using this mapping. This would result in:

ID      animal
AA10    dog
AA10    dog
AB11    cat
CC09    lion
CC09    lion

What is the best way to achieve this?

randomwalker
  • 183
  • 3
  • 11

1 Answers1

0

Here a solution with dplyr

library(dplyr)

df <- tibble(id = c("AA10", "AA10", "AB11", "CC09", "CC09"))

dict <- 
  tibble(
    id = c("AA10","AB11","CC09"),
    animal = c("dog","cat","lion")
)    

df %>% 
  left_join(dict)

Joining, by = "id"
# A tibble: 5 x 2
  id    animal
  <chr> <chr> 
1 AA10  dog   
2 AA10  dog   
3 AB11  cat   
4 CC09  lion  
5 CC09  lion 
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32