I have the following code, which works when I run it on its own:
library(tibble)
library(dplyr)
mytable <- tibble(Subgroup = c("C", "C", "A", "C"),
Category = c("A", "B", "D", "D"))
color_table <- tibble(Subgroup = c("A", "B", "C", "D"),
colors = c("#0079c0", "#cc9900", "#252525", "#c5120e"))
(mytable <- mutate(mytable, colors = color_table[[2]][match(mytable[[2]], color_table[[1]])]))
# A tibble: 4 × 3
Subgroup Category colors
<chr> <chr> <chr>
1 C A #0079c0
2 C B #cc9900
3 A D #c5120e
4 C D #c5120e
I now want to include this code into a higher function. I'm trying here with mutate_()
:
(mytable <- mutate_(mytable, .dots = list(colors = color_table[[2]][match(mytable[[2]], color_table[[Subgroup]])])))
# A tibble: 4 × 3
Subgroup Category colors
<chr> <chr> <chr>
1 C A NA
2 C B NA
3 A D NA
4 C D NA
It's appending the colors
variable, but it's not doing the matching correctly. Any tips on how I should adjust?