case_when 以两个字符列为条件的输出

case_when output conditioned by two character columns

我目前正在尝试填充基于两个字符列的列。

示例代码:

A <- structure(list(Name = c("Piece 1", "Piece 1", "Piece 1","Piece 1"), Size = c("S", 
                                                                        "M", "L", NA_character_), SKU = c(NA_character_, NA_character_, NA_character_,NA_character_
                                                                        )), row.names = c(NA, -4L), class = "data.frame")

这是我当前方法的预览,不太确定为什么它不响应这种情况。如果名称 == "X" 和大小 == "Y" ~ "自定义字段":

A <- A %>%
  mutate(Size = replace_na(Size, "OS")) %>%
  mutate(SKU = case_when(
    SKU == (Name == "Piece1" & 
                    Size == "S") ~ "PS",
    SKU == (Name == "Piece1" & 
                    Size == "M") ~ "PM",
    SKU == (Name == "Piece1" & 
                    Size == "L") ~ "PL",
    SKU == (Name == "Piece1" & 
                    Size == "OS") ~ "POS",
    TRUE ~ as.character(SKU)))

有什么建议吗?

SKU ==case_when 中似乎是不必要的。要匹配的文本也应该是准确的。您正在与 Name == "Piece1" 进行比较,但在数据中您有 "Piece 1"(带有 space)。

library(dplyr)

A %>%
  mutate(SKU = case_when(
    Name == "Piece 1" & Size == "S" ~ "PS",
    Name == "Piece 1" & Size == "M" ~ "PM",
    Name == "Piece 1" & Size == "L" ~ "PL",
    TRUE ~ as.character(SKU)))

#     Name Size SKU
#1 Piece 1    S  PS
#2 Piece 1    M  PM
#3 Piece 1    L  PL

为更新数据处理NA值-

A %>%
  mutate(SKU = replace(Size, is.na(Size), 'OS'),
         #tidyr::replace_na also works
         #SKU = tidyr::replace_na(Size, 'OS'),
         SKU = case_when(
           Name == "Piece 1" & Size == "S" ~ "PS",
           Name == "Piece 1" & Size == "M" ~ "PM",
           Name == "Piece 1" & Size == "L" ~ "PL",
           TRUE ~ as.character(SKU)))

#     Name Size SKU
#1 Piece 1    S  PS
#2 Piece 1    M  PM
#3 Piece 1    L  PL
#4 Piece 1 <NA>  OS