Home > OS >  R dates comparison using loop
R dates comparison using loop

Time:10-28

I have a dataframe with 1 date column (converted as.Date).

I am trying to write a loop to create a value in another column to "check" the death date compare to a fix value (today's date).

fix_date= as.Date(2021-10-28)

for (i in 1:length(df$Death.date)) {
  if (df$Death.date[i] < as.Date(fix_date)){
    df$death_check[i]<-"good"
  }
}

So for each row if Death.date < fix_date, fill death_check column with "good".

It is giving me this error code:

Error in if (new_possible_population$Death.date[i] < as.Date(exploratory_date)) { : missing value where TRUE/FALSE needed

Is this the correct way to code for the loop concerning date values? or is there a better way than using loops for this?

CodePudding user response:

You definitely want to use vectorised functions for this, check out the dplyr package:

df %>%
  mutate(death_check = case_when(Death.date < as.Date("2021-10-28") ~ "good"))

As you can see I added "" around the date as well, this is neccessary. If your df$Death.date is not actually in Date format you can change that here as well.

CodePudding user response:

library(data.table)
df <- data.table(
  Death.date = sample(seq(as.Date("2020-01-01"), by = "month", length.out = 25))
)

# just a TRUE for "good" which makes FALSE "bad"
df[, death_check_1 := Death.date < Sys.time()]

# written "good"
df[Death.date < Sys.time(), death_check_2 := "good"]

CodePudding user response:

Here's another option using sapply and an ifelse:

# make df using Merijn's code
df <- data.frame(Death.date = sample(seq(as.Date("2020-01-01"), 
                 by = "month", 
                 length.out = 25)))

# set the date to check against
fix_date <- as.Date("2021-10-28")

# make the comparison, return "good" or NA
df$death_check <- sapply(df$Death.date, function(x) {
                         ifelse(x < fix_date, "good", NA)
                         })

df
  • Related