问题描述
我在 df 中有此列:
Column1 |
---|
今天阳光明媚 |
晴天 |
今天阳光不是很好 |
期望输出
Column1 |
---|
晴天 |
晴天 |
不是晴天 |
df<-df%>%
mutate(column_1=case_when(
str_detect(column_1,"very sunny")~ "sunny",str_detect(column_1,"sunny")~ "sunny","not"&"sunny")~ " not sunny",)
)
该代码适用于前两行,其中第三行有更简单的条件,但条件更复杂并给我一个错误。
我想确定该字符串中的一些关键字,它们不在一起(非常晴朗)但它们是分开的(今天不是非常晴朗),并将它们作为条件以提供所需的输出。也许我在语法上做错了。
解决方法
df$column2 <- sub('(not )?.*(sunny).*','\\1\\2',df$Column1)
df
Column1 column2
1 very sunny day today sunny
2 it was sunny sunny
3 not very sunny today not sunny
,
第三行是&&而不是&
,试试这个。
df <- data.frame(column_1 = c("very sunny today","sunny today","not very sunny today","very very sunny today","sunny today,not","not sunny today","no sun today"))
df%>%
mutate(column_1 = case_when(
(str_detect(column_1,"not") & str_detect(column_1,"sunny")) ~ "not sunny",str_detect(column_1,"very sunny")~ "sunny","sunny")~ "sunny",TRUE ~ "Unspecified"
)
)
它处理not出现在sunny之前或之后,并由零个或多个单词分隔。我在您的测试数据框中添加了更多示例。最好将 TRUE ~ ""
语句包含到 case_when()
中,除非您确定将捕获所有可能的输入。