rdplyrcase-when

case_when output conditioned by two character columns


I am currently trying to populate a column based on two character columns.

Sample Code:

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")

This is a preview of my current approach, not really sure why it is not responding to this condition. If the Name == "X" and the Size == "Y" ~ "Custom Field":

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)))

Any suggestions?


Solution

  • SKU == seems unnecessary in case_when. Also text to be matched should be exact. You are comparing with Name == "Piece1" but in data you have "Piece 1" (with a 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
    

    For the updated data to handle NA values -

    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