簡體   English   中英

如何使用 mutate_at 和 case_when 為不同的列填充不同的值?

[英]How do you use mutate_at with case_when for different columns to fill different values?

我有一個包含多列的數據框(demo_df),我正在嘗試根據某些條件對兩個新列進行 append。 如果滿足條件,這兩列將填充不同的值 我在 mutate_at 中使用 case_when 來改變兩個變量。

  1. 查詢文本
  2. 查詢狀態

我該怎么做呢? 到目前為止,我有以下代碼。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM