Nesting ifelse() within mutate for a variable

Hi everyone.

I had prior success in creating an ifelse() statement for populating a column based on conditions for two variables (thanks to @andresrcs). However, I am now stuck on nesting ifelse() to set more conditions based on the same observation conditions as the first ifelse() statement.

I'm assigning "YES" or "NO" in a column based on "PATIENT_ID" and "DATE", with multiple patients being present in the data frame. Here is what I have attempted so far.

#Loading relevant packages
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.5.3
#> Warning: package 'ggplot2' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'readr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'dplyr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
#> Warning: package 'forcats' was built under R version 3.5.3
library(dplyr)
library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.5.3
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

#Extracted variables and observations from my data frame in question
PATIENT_ID <- c("-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483645", "-2147483645", "-2147483645", "-2147483645")
DATE <- c("06-08-2018", "20-08-2018", "01-10-2018", "04-02-2019","22-07-2019", "16-08-2018", "30-08-2018", "11-10-2018", "27-02-2019")
VISIT_DAY <- ""

#Example data frame with extracted variables and observations
df1 <- data.frame(PATIENT_ID, DATE, VISIT_DAY)

df1$DATE <- as.Date(df1$DATE, format = "%d-%m-%Y") #Converting to date

#Successful code to populate "YES" for met conditions of 1st patient
df1 <- df1 %>%
  mutate(VISIT_DAY = if_else((PATIENT_ID == "-2147483646" & DATE %in% dmy(c("06-08-2018", "20-08-2018", "01-10-2018", "04-02-2019","22-07-2019"))), "YES", "NO"))

#Unsuccessful nesting, still works for first patient but not the second
df1 <- df1 %>%
  mutate(VISIT_DAY = if_else((PATIENT_ID == "-2147483646" & DATE %in% dmy(c("06-08-2018", "20-08-2018", "01-10-2018", "04-02-2019","22-07-2019"))), "YES", "NO",
                             if_else((PATIENT_ID == "-2147483645" & DATE %in% dmy(c("16-08-2018", "30-08-2018", "11-10-2018", "27-02-2019"))), "YES", "NO")))

Any help with this would be much appreciated. Thanks!

Use case_when();

2 Likes

Nesting if_else() is not a good idea, use case_when() instead, also, you don't need to load dplyr separately, it is already loaded as part of the core tidyverse packages.

library(tidyverse)
library(lubridate)

#Example data frame with extracted variables and observations
df1 <- data.frame(
  PATIENT_ID = as.factor(c("-2147483646",
                           "-2147483646","-2147483646","-2147483646","-2147483646",
                           "-2147483645","-2147483645","-2147483645",
                           "-2147483645")),
        DATE = as.Date(c("06-08-2018",
                           "20-08-2018","01-10-2018","04-02-2019","22-07-2019",
                           "16-08-2018","30-08-2018","11-10-2018","27-02-2019"),
                       format = "%d-%m-%Y"),
   VISIT_DAY = as.factor(c(NA, NA, NA, NA, NA, NA, NA, NA, NA))
)


df1 %>%
    mutate(VISIT_DAY = case_when(
        PATIENT_ID == "-2147483646" & DATE %in% dmy(c("06-08-2018", "20-08-2018", "01-10-2018", "04-02-2019","22-07-2019")) ~ "YES",
        PATIENT_ID == "-2147483645" & DATE %in% dmy(c("16-08-2018", "30-08-2018", "11-10-2018", "27-02-2019")) ~ "YES",
        TRUE ~ "NO"))
#>    PATIENT_ID       DATE VISIT_DAY
#> 1 -2147483646 2018-08-06       YES
#> 2 -2147483646 2018-08-20       YES
#> 3 -2147483646 2018-10-01       YES
#> 4 -2147483646 2019-02-04       YES
#> 5 -2147483646 2019-07-22       YES
#> 6 -2147483645 2018-08-16       YES
#> 7 -2147483645 2018-08-30       YES
#> 8 -2147483645 2018-10-11       YES
#> 9 -2147483645 2019-02-27       YES

Created on 2020-01-31 by the reprex package (v0.3.0)

2 Likes

Forgive me if I am way off, but it seems unlikely to have a usecase where you would so heavily hardcode matching conditions, seems to me more likely that assuming this is for a work or research related project, that you would have some table of known patients, and then some other data of recent patient visits, and you might wish to join them together to see matches. That would be using dplyr functions like left_join or inner_join for example. Something to consider.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.