Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently my code is this:

  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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading