[英]Using case_when() within mutate_at() to recode rows of selected columns with different types with NA
[英]How do you use mutate_at with case_when for different columns to fill different values?
我有一個包含多列的數據框(demo_df),我正在嘗試根據某些條件對兩個新列進行 append。 如果滿足條件,這兩列將填充不同的值。 我在 mutate_at 中使用 case_when 來改變兩個變量。
我該怎么做呢? 到目前為止,我有以下代碼。
queries <- demo_df%>%
mutate_at(vars(c(QueryText, QueryStatus)) = case_when(
VISIT == "SCRN" & nles <=3 ~ {"Respond","High"},
VISIT == "D1" & nles >3 ~ {"Ignore","Low"}))
您不能在一個case_when
語句中分配多個列。 嘗試:
library(dplyr)
queries <- demo_df %>%
mutate(QueryText = case_when(
VISIT == "SCRN" & nles <=3 ~ "Respond"},
VISIT == "D1" & nles >3 ~ "Ignore"),
QueryStatus = case_when(QueryText == 'Respond' ~ 'High',
QueryText == 'Ignore' ~ 'Low'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.