R - check if string contains dates within specific date range

后端 未结 2 707
滥情空心
滥情空心 2020-12-11 07:10

I have the following requirement in R script (to write a Expression function in Spotfire):

dateString <- \"04/30/2015 03/21/2015 06/28/2015 12/19/2015\"
s         


        
2条回答
  •  再見小時候
    2020-12-11 07:52

    Here's an alternative solution without additional packages.

    First, represent strings as dates:

    dates <- lapply(strsplit(dateString, " +")[[1L]], as.Date, "%m/%d/%Y")
    start <- as.Date(startDate, "%m/%d/%Y")
    end <- as.Date(endDate, "%m/%d/%Y")
    

    Second, check whether the dates are between start and end:

    sapply(dates, function(x) x >= start && x <= end)
    # [1]  TRUE FALSE  TRUE FALSE
    

提交回复
热议问题