Use date over and over for filtering

i have a DF like this:
structure(list(TBOutcomeDate = c("NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL"
), DateOfBirth = c("2003-02-11", "1978-08-25", "1975-06-28",
"1983-01-03", "1959-08-10", "1955-07-28", "1998-12-26", "1972-03-27",
"1961-04-19", "1982-10-15", "1985-06-27", "1963-03-28", "1945-10-14",
"1985-12-14", "1985-03-22", "1953-12-27", "1990-03-18", "2021-08-01",
"1979-06-04", "2005-04-30"), FirstVisitDate = c("NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "2017-01-30", "NULL",
"2018-11-06", "2018-01-24", "NULL", "NULL", "NULL", "NULL", "NULL",
"2018-07-02", "NULL", "NULL", "NULL"), Counselling1 = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), Counselling2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Counselling3 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), CounsellingS = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), PapSmearDate = c("NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "2006-04-18", "NULL"), COTStartedDate = c("NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"2006-07-24", "NULL"), SecondLineDate = c("NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL"), OutcomeDate = c("NULL", "NULL", "2019-09-20", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "2019-01-25", "NULL", "2006-09-23",
"NULL")), row.names = c(NA, 20L), class = "data.frame")

I want to declare dates as follows:
Cohort_SDate <- ymd("2023/01/01")
Cohort_EDate <- ymd("2023/03/31")

And filter somewhere in the script like:
IPTLIst <- subset(EX, IPTStartDate >= Cohort_SDate & IPTStartDate <= Cohort_EDate)

But I get an error as follows:
Error in charToDate(x) :
character string is not in a standard unambiguous format

Could I get an assistance on this. I just do not want to type dates over and over again

1 Like

Hi @Itu ,

your column IPTStartDate is very probably not transformed into a date format as you did with Cohort_SDate and Cohort_EDate .

You can reproduce that by doing (df is your data frame as stated):

library(lubridate)
df$FirstVisitDate >Cohort_EDate
# results in the error you stated
df$FirstVisitDate <- ymd(df$FirstVisitDate)
# you will get warnings since your NULL values are strings but will still work
df$FirstVisitDate >Cohort_EDate
# will work as you espect
1 Like

As @ vedoa says.

However, your example data.frame does not contain contain the variable * IPTStartDate*.

1 Like

Thanks. It worked i did not realise I left out the IPT Date

That worked very well Sir. Thank you

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.