Home > database >  Create a function with multiple conditionals to assign values to a column in R dataframe
Create a function with multiple conditionals to assign values to a column in R dataframe

Time:08-24

I have a Dataframe that looks like this in R:

df

date location daytype season shift
2022-9-1 NT Thur spring morning
2022-9-2 NT Fri summer morning
2022-9-3 AP Sat summer afternoon
2022-9-4 AP Sun fall morning
2022-9-5 NT Mon winter afternoon

I want to create a new column for an end time for the shift depending on the shift and season in the dataframe.

Currently my code is this:

endtime = function(x){
  if(x %in% "Spring" & shift %in% "morning") return("01:00 PM")
  if(x %in% "Summer" & shift %in% "morning") return("01:30 PM")
  if(x %in% "Fall" & shift %in% "morning") return("01:00 PM")
  if(x %in% "Winter" & shift %in% "morning") return("12:30 PM")
  if(x %in% "Spring" & shift %in% "afternoon") return("06:00 PM")
  if(x %in% "Summer" & shift %in% "afternoon") return("07:00 AM")
  if(x %in% "Fall" & shift %in% "afternoon") return("06:00 PM")
  if(x %in% "Winter" & shift %in% "afternoon") return("05:00 PM")
}

df$endtime = sapply(calendar$Season, endtime)

But you cannot have more than two conditions with an if function. Is there a way to do this?

CodePudding user response:

if/else is not vectorized. We may either use ifelse or case_when

library(dplyr)
df %>% 
    mutate(endtime = case_when(
      Season %in% "Spring" & shift %in% "morning" ~ "01:00 PM",
      Season %in% "Summer" & shift %in% "morning"~ "01:30 PM",
      ...
       
     ))

Or another option is to create a key/val dataset and do a join

keyval <- tibble(Season = c("Spring", "Summer"), 
  shift = c("morning", "morning"), endtime = c("01:00 PM", "01:30 PM"))
left_join(df, keyval)
  • Related