Noobie Noobie - 1 month ago 9
R Question

dplyr: how to (concisely) use a mutate conditional on different cases?

Consider the following example

data <- data_frame(name = c('A','B','C','C',NA,'D'))

> data
# A tibble: 6 × 1
name
<chr>
1 A
2 B
3 C
4 C
5 <NA>
6 D


Here, I know that the variable
name
actually maps to
'A' -> 'one'
and
'B' -> 'two'
. I would simply like to create a variable that gets the mapping value. Of course, in my original dataset I have many more cases to map.

Something that does not work is the following.

data <- data %>%
mutate(mapping = ifelse(name == 'A', 'one', name),
mapping = ifelse(name == 'B', 'two', name))
> data
# A tibble: 6 × 2
name mapping
<chr> <chr>
1 A A
2 B two
3 C C
4 C C
5 <NA> <NA>
6 D D


What is wrong here? What is the most efficient way to do so in
dplyr
?

Many thanks!

Answer

If you want to avoid nested ifelse , you should simply create a mapping data frame and inner join with it .

mapping_df <- data.frame(name = c('A', 'B', 'C' . . . . 'Z'), mapping = c(1:26))

left_join(data, mapping_df, by = "name")