与状态的突变弦
如果
status <- c("Open", "In Progress", "DevTest", "Stage Test: mw", "Stage Test: customer", "DevDone", "Done")
a <- c("Open, Open")
b <- c("Open, In Progress, DevTest, DevTest")
c <- c("DevTest, Done")
d <- c("Done, Done")
data <- tibble(status = c(a, b, c, d))
现在,
- 状态仅包含“打开” - &gt;如果
- 状态包含“正在进行的” 或“ DevTest” - &gt; 如果状态
- 仅包含 “完成” - &gt; “完成”,
因此结果看起来应该像
状态 | _simple |
---|---|
打开,打开 | 打开 |
,正在进行中,devtest,devtest | 正在进行的 |
devtest, | 在进行中 |
完成,完成 | 了 |
Given the following example
status <- c("Open", "In Progress", "DevTest", "Stage Test: mw", "Stage Test: customer", "DevDone", "Done")
a <- c("Open, Open")
b <- c("Open, In Progress, DevTest, DevTest")
c <- c("DevTest, Done")
d <- c("Done, Done")
data <- tibble(status = c(a, b, c, d))
Now I want mutate an additional column with the following condition
- If status only contains "Open" -> Open
- If status contains "In Progress" or "DevTest" -> "In Progress"
- If status contains only "Done" -> "Done"
So the result should look like
status | status_simple |
---|---|
Open, Open | Open |
Open, In Progress, DevTest, DevTest | In Progress |
DevTest, Done | In Progress |
Done, Done | Done |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,
case_when
可以非常有用,可以通过条件进行分类及其后果,并结合grepl
,以搜索字符串中的模式(并返回布尔矢量,这是case_when
期望的):结果:
This is a case where
case_when
can be very useful, to sort through the conditions and their consequence, in combination withgrepl
, to search for patterns in the character strings (and returns boolean vectors, which is whatcase_when
expects):And the result: