在以下数据框中:
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个
我正在尝试将注释中的值替换为:
could be turned into a double
-> "3"
grepl("pos",tolower(notes))
-> "2"
grepl("neg",tolower(notes))
-> "1"
"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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句