我正在尝试使用str_detect和case_when根据多种模式对字符串进行重新编码,并将每次出现的重新编码值粘贴到新列中。 “正确”列是我要实现的输出。
这类似于this question和this question如果用case_when无法完成(我认为仅限于一种模式),还有更好的方法可以仍然使用tidyverse吗?
Fruit=c("Apples","apples, maybe bananas","Oranges","grapes w apples","pears")
Num=c(1,2,3,4,5)
data=data.frame(Num,Fruit)
df= data %>% mutate(Incorrect=
paste(case_when(
str_detect(Fruit, regex("apples", ignore_case=TRUE)) ~ "good",
str_detect(Fruit, regex("bananas", ignore_case=TRUE)) ~ "gross",
str_detect(Fruit, regex("grapes | oranges", ignore_case=TRUE)) ~ "ok",
str_detect(Fruit, regex("lemon", ignore_case=TRUE)) ~ "sour",
TRUE ~ "other"
),sep=","))
Num Fruit Incorrect
1 Apples good
2 apples, maybe bananas good
3 Oranges other
4 grapes w apples good
5 pears other
Num Fruit Correct
1 Apples good
2 apples, maybe bananas good,gross
3 Oranges ok
4 grapes w apples ok,good
5 pears other
答案 0 :(得分:3)
在case_when
中,如果满足某一行的条件,它将在此处停止,并且不再检查其他条件。因此,通常在这种情况下,最好将每个条目都放在单独的行中,以便更轻松地分配值,然后summarise
将它们全部在一起。但是,在这种情况下,Fruit
列没有明确的分隔符,有些果实用逗号(,
)分隔,有些果实带有空格,并且它们之间还有其他单词。为了处理所有此类情况,我们将NA
分配给不匹配的单词,然后在汇总过程中将其删除。
library(dplyr)
library(stringr)
data %>%
tidyr::separate_rows(Fruit, sep = ",|\\s+") %>%
mutate(Correct = case_when(
str_detect(Fruit, regex("apples", ignore_case=TRUE)) ~ "good",
str_detect(Fruit, regex("bananas", ignore_case=TRUE)) ~ "gross",
str_detect(Fruit, regex("grapes|oranges", ignore_case=TRUE)) ~ "ok",
str_detect(Fruit, regex("lemon", ignore_case=TRUE)) ~ "sour",
TRUE ~ NA_character_)) %>%
group_by(Num) %>%
summarise(Correct = toString(na.omit(Correct))) %>%
left_join(data)
# Num Correct Fruit
# <dbl> <chr> <fct>
#1 1 good Apples
#2 2 good, gross apples, maybe bananas
#3 3 ok Oranges
#4 4 ok, good grapes w apples
#5 5 sour Lemons
对于更新的数据,我们可以删除出现并执行的多余单词
data %>%
mutate(Fruit = gsub("maybe|w", "", Fruit)) %>%
tidyr::separate_rows(Fruit, sep = ",\\s+|\\s+") %>%
mutate(Correct = case_when(
str_detect(Fruit, regex("apples", ignore_case=TRUE)) ~ "good",
str_detect(Fruit, regex("bananas", ignore_case=TRUE)) ~ "gross",
str_detect(Fruit, regex("grapes|oranges", ignore_case=TRUE)) ~ "ok",
str_detect(Fruit, regex("lemon", ignore_case=TRUE)) ~ "sour",
TRUE ~ "other")) %>%
group_by(Num) %>%
summarise(Correct = toString(na.omit(Correct))) %>%
left_join(data)
# Num Correct Fruit
# <dbl> <chr> <fct>
#1 1 good Apples
#2 2 good, gross apples, maybe bananas
#3 3 ok Oranges
#4 4 ok, good grapes w apples
#5 5 other pears