gpt4 book ai didi

替换匹配多个不同模式的列值

转载 作者:行者123 更新时间:2023-12-04 01:26:10 24 4
gpt4 key购买 nike

在下面的数据框中:

library(tidyverse)
df <- tibble(notes=c("Positive result","Negative","NEG","POS >2","pOS","Cannot Determine","2.4","3.1","0.2"))

notes
<chr>
1 Positive result
2 Negative
3 NEG
4 POS >2
5 pOS
6 Cannot Determine
7 2.4
8 3.1
9 0.2

我想定义一个单行代码来替换注释列中与模式匹配的条目。如果只有两个条件,我会使用三元运算符。但这里我有 5 个。

我希望将注释中的值替换为:

  1. 可以变成双数 -> "3"
  2. grepl("pos",tolower(notes)) -> "2"
  3. grepl("neg",tolower(notes)) -> "1"
  4. 以上都不是 -> “0”

我最初做了:

df %>%
mutate(notes=ifelse(grepl("[[:digit:]]+",notes)),"3",notes) %>% # could be coerced into a double
mutate(notes=ifelse(grepl("pos",tolower(notes))),"2",notes) %>% # contains "pos"
mutate(notes=ifelse(grepl("neg",tolower(notes))),"1",notes) %>% # contains "neg"
mutate(notes=ifelse(grepl("3|2|1",tolower(notes))),notes,"0") %>% # none of the above
type.convert()

期望的输出

notes           
<dbl>
1 2
2 1
3 1
4 2
5 2
6 0
7 3
8 3
9 3

最佳答案

我们可以使用case_when

library(dplyr)
library(stringr)
df %>%
mutate(notes1 = toupper(substr(notes, 1, 3)),
notes =case_when(notes1 == "POS" ~ 2,
notes1 == 'NEG' ~ 1,
str_detect(notes, '^[0-9.]+$')~ 3,
TRUE ~ 0)) %>%
select(-notes1)
# A tibble: 9 x 1
# notes
# <dbl>
#1 2
#2 1
#3 1
#4 2
#5 2
#6 0
#7 3
#8 3
#9 3

如果我们需要保持数值不变,一种选择是 as.numeric 然后是 coalesce

df %>% 
mutate(notes1 = toupper(substr(notes, 1, 3)),
notes2 =case_when(notes1 == "POS" ~ 2,
notes1 == 'NEG' ~ 1,
str_detect(notes, '^[0-9.]+$')~ 3,
TRUE ~ 0)) %>%
select(-notes1) %>%
mutate(notes = coalesce(as.numeric(notes), notes2)) %>%
select(-notes2)

关于替换匹配多个不同模式的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61902387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com