Salma Abdel-Raheem
Salma Abdel-Raheem

Reputation: 91

as.Date character string with month-day data

Hi I am trying to determine whether my data points occurred during a proposed work season. I have this function:

### Get whether or not sighting occurred during the proposed work period
getWork <-function(w) {
    startWork <- as.Date("07-15", format = "%m-%d") #Start of stated work period
    endWork <- as.Date("02-15", format = "%m-%d") #End of stated work period

  ifelse (w >=startWork & w <= endWork, 1, 0)
  }

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- format(sightingsData$SightDate, format="%m-%d")
sightingsData$workPeriod <- getWork(sightingsData$SightMonthDay) #Get Work T/F values for each date

The code for the function runs smoothly without errors, but I get an error for the output, as follows:

Error in charToDate(x) : 
  character string is not in a standard unambiguous format 
6. stop("character string is not in a standard unambiguous format") 
5. charToDate(x) 
4. as.Date.character(e1) 
3. as.Date(e1) 
2. `>=.Date`(w, startWork) 
1. getWork(sightingsData$SightMonthDay)

However, if I change the syntax of the function to match that of creating the 'SightMonthDay' column, (i.e. startwork <- as.Date.... instead of startwork <- format....)I get an output, but all of the 'getWork' values are apparently between -1 and 0, however all of the observations are considered 0 [False] which out of over 23500 observations is not true. Here is the code to illustrate what I mean:

getWork <-function(w) {
    startWork <- format("07-15", format = "%m-%d") #Start of stated work period
    endWork <- format("02-15", format = "%m-%d") #End of stated work period
    +(w >=startWork & w <= endWork)
}

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- format(sightingsData$SightDate, format="%m-%d")
sightingsData$workPeriod <- getWork(sightingsData$SightMonthDay) #Get Work T/F values for each date

And here's a little screenshot to show what I mean...

enter image description here

Here's a sample dataset:

       SightDate SightMonthDay workPeriod
18163 2017-11-15         11-15          0
17120 2017-10-23         10-23          0

> dput(droplevels(SampleData[1:20, ]))
structure(list(SightDate = structure(c(17485, 17462, 17469, 17417, 
17871, 17294, 16712, 15069, 16576, 17416, 17515, 16407, 16399, 
17298, 17164, 17244, 17464, 15628, 16388, 16598), class = "Date"), 
    SightMonthDay = c("11-15", "10-23", "10-30", "09-08", "12-06", 
    "05-08", "10-04", "04-05", "05-21", "09-07", "12-15", "12-03", 
    "11-25", "05-12", "12-29", "03-19", "10-25", "10-15", "11-14", 
    "06-12"), workPeriod = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(18163L, 
17120L, 17437L, 15597L, 23402L, 14359L, 9874L, 2902L, 8910L, 
15585L, 19641L, 7769L, 7631L, 14399L, 13163L, 13447L, 17297L, 
4511L, 7405L, 9113L), class = "data.frame")

Thank you for your time and help!!

Upvotes: 0

Views: 116

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

Probably, your startWork and endWork are replaced.

getWork <-function(w) {
   endWork <- as.Date("07-15", format = "%m-%d") #Start of stated work period
   startWork <- as.Date("02-15", format = "%m-%d") #End of stated work period
   +(w >=startWork & w <= endWork)
}

and apply it to SightMonthDay.

SampleData$new_date <- as.Date(format(SampleData$SightDate, "%m-%d"), "%m-%d")
getWork(SampleData$new_date)

#[1] 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 1

Upvotes: 1

Related Questions