问题
I have this data which I want to replace each value with a fixed value from another list
values
key1
key3;key4;key1
key2;key1
Here is the list that "translates" each key into something else (these are just examples so it is not enought to just change the word "key" to "code".
key code
key1 code1
key2 code2
key3 code3
key4 code4
So the end product should look like this:
values
code1
code3;code4;code1
code2;code1
This is how I tried to solve it but got stuck:
values = tibble(id = 1:3, values = c("key1", "key3;key4;key1", "key2;key1"))
key_code = tibble(key = c("key1", "key2", "key3", "key4"), code = c("code1", "code2", "code3", "code4"))
values %>%
mutate(values = strsplit(values, ";")) %>%
unnest(values) %>%
left_join(key_code, by = c("values" = "key"))
This is what I get
# A tibble: 6 x 2
id code
<int> <chr>
1 1 code1
2 2 code3
3 2 code4
4 2 code1
5 3 code2
6 3 code1
From here I want to get:
id code
<int> <chr>
1 1 code1
2 2 code3;code4;code1
3 3 code2;code1
回答1:
We can use paste or str_c. Instead of strsplit, another option is separate_rows
library(dplyr)
library(tidyr)
library(stringr)
values %>%
separate_rows(values) %>%
left_join(key_code, by = c("values" = "key")) %>%
group_by(id) %>%
summarise(code = str_c(code, collapse=";"))
# A tibble: 3 x 2
# id code
# <int> <chr>
#1 1 code1
#2 2 code3;code4;code1
#3 3 code2;code1
Or an easier option is str_replace which can take a named vector or list and use that to replace the values in the string without even splitting or joining
values %>%
mutate(values = str_replace_all(values, set_names(key_code$code, key_code$key)))
# A tibble: 3 x 2
# id values
# <int> <chr>
#1 1 code1
#2 2 code3;code4;code1
#3 3 code2;code1
来源:https://stackoverflow.com/questions/61447144/how-to-replace-multiple-values-in-a-string-depending-on-a-list-of-keys