我有一个数据框。我想编写条件来创建依赖于其他列的列。在这里:
tab <- tibble::tribble(
~dataset_id, ~type,
"Site4H", 268,
"Site4D", 479,
"SIte8H", 345,
"Site8D", 567,
"Site8K", blond507
)
library(dplyr)
tab %>%
mutate(state = case_when(
endsWith(dataset_id, "H") ~ "healthy",
endsWith(dataset_id, "D") ~ "disease",
TRUE ~ NA_character_
))
如您所见,如果列dataset_id中的值以H结尾,则列状态等于健康。但是我希望它在两种情况下是健康的:当dataset_id列中的值以H结尾并且当“类型”列中的值以“ blond”开头时”。我该怎么办?我需要使用这些确切的函数,因此使用其他库解决方案不好。
所需的结果是:
dataset_id type state
Site4H 268 healthy
Site4D 479 disease
SIte8H 345 healthy
Site8D 567 disease
Site8K blond507 healthy
答案 0 :(得分:0)
tab <- tibble::tribble(
~dataset_id, ~type,
"Site4H", "268",
"Site4D", "479",
"SIte8H", "345",
"Site8D", "567",
"Site8K", "blond507"
)
tab %>%
mutate(state = case_when(
endsWith(dataset_id, "H") | startsWith(type, "blond") ~ "healthy",
endsWith(dataset_id, "D") ~ "disease",
TRUE ~ NA_character_
))
因此,您只需在case_when代码中添加类型限制即可:
# A tibble: 5 x 3
dataset_id type state
<chr> <chr> <chr>
1 Site4H 268 healthy
2 Site4D 479 disease
3 SIte8H 345 healthy
4 Site8D 567 disease
5 Site8K blond507 healthy
两个注意事项: