当需要满足多个条件时,使用 case_when 创建新列
creating new column with case_when when multiple conditions need to be satisfied
我正在为满足 3 个条件的行创建一个指示器:1) 检测到字符串 & 2) 第二个字符 == 4 & 3) 年龄 >=18。
我不明白为什么不满足条件 1 时第 3 行会被标记。
数据:
df <- data.frame(
id=c(1,1,2,2,3,3),
record=as.character(c("A4B", "AAA", "B45", "BBB", "B4A4", "CA4")),
age=c(12,12,18,18,22,22))
code_flag <- c("A4", "BBB")
> df
id record age
1 1 A4B 12
2 1 AAA 12
3 2 B45 18
4 2 BBB 18
5 3 B4A4 22
6 3 CA4 22
代码:
library(dplyr)
library(stringr)
df %>%
mutate(
flag=case_when(any(str_detect(record, code_flag)) & str_sub(record, start=2, end=2) == "4" & age >=18 ~ "yes", TRUE ~ "no")
)
输出:
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 **yes**
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no
期望的输出:
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 no
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no
这行得通吗:
df %>%
mutate(
flag=case_when(str_detect(record, str_c(code_flag, collapse = '|')) & str_sub(record, start=2, end=2) == "4" & age >=18 ~ "yes", TRUE ~ "no")
)
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 no
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no
我正在为满足 3 个条件的行创建一个指示器:1) 检测到字符串 & 2) 第二个字符 == 4 & 3) 年龄 >=18。
我不明白为什么不满足条件 1 时第 3 行会被标记。
数据:
df <- data.frame(
id=c(1,1,2,2,3,3),
record=as.character(c("A4B", "AAA", "B45", "BBB", "B4A4", "CA4")),
age=c(12,12,18,18,22,22))
code_flag <- c("A4", "BBB")
> df
id record age
1 1 A4B 12
2 1 AAA 12
3 2 B45 18
4 2 BBB 18
5 3 B4A4 22
6 3 CA4 22
代码:
library(dplyr)
library(stringr)
df %>%
mutate(
flag=case_when(any(str_detect(record, code_flag)) & str_sub(record, start=2, end=2) == "4" & age >=18 ~ "yes", TRUE ~ "no")
)
输出:
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 **yes**
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no
期望的输出:
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 no
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no
这行得通吗:
df %>%
mutate(
flag=case_when(str_detect(record, str_c(code_flag, collapse = '|')) & str_sub(record, start=2, end=2) == "4" & age >=18 ~ "yes", TRUE ~ "no")
)
id record age flag
1 1 A4B 12 no
2 1 AAA 12 no
3 2 B45 18 no
4 2 BBB 18 no
5 3 B4A4 22 yes
6 3 CA4 22 no