与状态的突变弦

发布于 2025-01-20 07:48:05 字数 1054 浏览 0 评论 0原文

如果

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

statusstatus_simple
Open, OpenOpen
Open, In Progress, DevTest, DevTestIn Progress
DevTest, DoneIn Progress
Done, DoneDone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2025-01-27 07:48:05

在这种情况下,case_when可以非常有用,可以通过条件进行分类及其后果,并结合grepl,以搜索字符串中的模式(并返回布尔矢量,这是case_when期望的):

data %>% mutate(
  status_simple = case_when(
    grepl("DevTest|In Progress", status) ~ "In Progress",
    grepl("Open", status) ~ "Open",
    grepl("Done", status) ~ "Done",
    ))

结果:

# A tibble: 4 × 2
  status                              status_simple
  <chr>                               <chr>       
1 Open, Open                          Open        
2 Open, In Progress, DevTest, DevTest In Progress 
3 DevTest, Done                       In Progress 
4 Done, Done                          Done   

 

This is a case where case_when can be very useful, to sort through the conditions and their consequence, in combination with grepl, to search for patterns in the character strings (and returns boolean vectors, which is what case_when expects):

data %>% mutate(
  status_simple = case_when(
    grepl("DevTest|In Progress", status) ~ "In Progress",
    grepl("Open", status) ~ "Open",
    grepl("Done", status) ~ "Done",
    ))

And the result:

# A tibble: 4 × 2
  status                              status_simple
  <chr>                               <chr>       
1 Open, Open                          Open        
2 Open, In Progress, DevTest, DevTest In Progress 
3 DevTest, Done                       In Progress 
4 Done, Done                          Done   

 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文