Search code examples
rif-statementdplyrlubridateas.date

If else statements for dates in R


Hi I am trying to create a new column in my data frame to say "if 'SightDate' is between 7-15 and 2-15 return TRUE 1 else FALSE [0]" but can't seem to find the syntax for date functions in r. This is what I have so far.

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- strptime(as.character(sightingsData$SightDate), "%m/%d/%Y")
sightingsData$SightMonthDay <- format.Date(sightingsData$SightMonthDay, "%m-%d")

#Get whether or not sighting occured during the proposed work period
startWork <- as.Date("07-15", format = "%m-%d")
endWork <- as.Date("02-15", format = "%m-%d")

sightingsData$WorkPeriod = ifelse(sightingsData$SightMonthDay >= startWork & sightingsData$SightMonthDay <= endWork, 1, 0)

I get this error

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

I'm not sure why, because when I take out the comma, I instead get "unexpected numeral 1".

Additionally, I am trying to assign seasons to my data using a function that I found on a forum. But I'm struggling with date assignments in R and can't seem to make sense of it. I want to be able to contain the seasons assignment into a new column titled "SightSeason" or something like that.

sightingsData$sightSeason <- getSeason(sightingsData$SightMonthDay)

But can't seem to see where it is that I would account for that in the function below...before or after I convert my sightingsData$SightData to 2016 values or after. This makes me hesitant, and I wonder if I should create an intermediate column in which I can store the conversion of my sightingsData$SightDate to 2016 values -- something like, sightingsData$2016Sight so that I don't permanently alter my df. I'm not really clear on this. I apologize for rambling, but I'm not sure how to clarify my issues. Anywho, this is the "get seasons function":

### Assign SightMonthDay to Season using 2016 Season Data
getSeason <- function(DATES)
  WS <- as.Date("2016-12-21", format = "%Y-%m-%d") # Winter Solstice
  SE <- as.Date("2016-3-19",  format = "%Y-%m-%d") # Spring Equinox
  SS <- as.Date("2016-6-20",  format = "%Y-%m-%d") # Summer Solstice
  FE <- as.Date("2016-9-22",  format = "%Y-%m-%d") # Fall Equinox

# Convert dates from any year to 2016 dates
d <- as.Date(strftime(DATES, format="2016-%m-%d"))

ifelse (d >= WS | d < SE, "Winter",
        ifelse (d >= SE & d < SS, "Spring",
                ifelse (d >= SS & d < FE, "Summer", "Fall")))

Any insight as to how I can apply that function, or if you know of a different way to assign seasons to my dataset that would be much appreciated! To clarify, I'm using 2016 because it was the last leap year for which there is complete solstice and equinox data.

This is a screenshot of a bit of my df, it has a total of 23585 observations.

This is a 20-observation random sample of my data:

       SightDate SightMonthDay
17092 10/23/2017         10-23
129    3/13/2009         03-13
20671  1/17/2018         01-17

And the output of dput(SampleData)

> dput(droplevels(SampleData[1:20, ]))
structure(list(SightDate = structure(c(1L, 16L, 18L, 7L, 8L, 
19L, 10L, 6L, 9L, 14L, 13L, 5L, 15L, 2L, 3L, 17L, 4L, 11L, 12L, 
20L), .Label = c("10/13/2015", "10/28/2017", "11/10/2018", "11/2/2018", 
"11/29/2012", "12/14/2017", "12/21/2013", "12/3/2016", "12/5/2017", 
"3/14/2016", "3/22/2015", "3/25/2011", "4/15/2018", "4/4/2014", 
"5/1/2017", "6/26/2016", "8/18/2015", "9/15/2017", "9/18/2015", 
"9/18/2017"), class = "factor"), SightMonthDay = c("10-13", "06-26", 
"09-15", "12-21", "12-03", "09-18", "03-14", "12-14", "12-05", 
"04-04", "04-15", "11-29", "05-01", "10-28", "11-10", "08-18", 
"11-02", "03-22", "03-25", "09-18")), row.names = c(9977L, 11703L, 
15804L, 6177L, 12954L, 9707L, 10774L, 19559L, 18897L, 6546L, 
21349L, 4797L, 14169L, 17403L, 23014L, 9410L, 22758L, 8440L, 
2854L, 15886L), class = "data.frame")

Apologies, I'm not totally clear on how to post a sample of my data for others to manipulate.

Thank you for your time!


Solution

  • You don't need SightMonthDay column.

    #Function to get season
    getSeason <- function(d) {
       WS <- as.Date("2016-12-21") # Winter Solstice
       SE <- as.Date("2016-3-19") # Spring Equinox
       SS <- as.Date("2016-6-20") # Summer Solstice
       FE <- as.Date("2016-9-22") # Fall Equinox
    
     ifelse (d >= WS | d < SE, "Winter",
          ifelse (d >= SE & d < SS, "Spring",
                  ifelse (d >= SS & d < FE, "Summer", "Fall")))
    }
    #Change to standard date format
    SampleData$date <- as.Date(SampleData$SightDate, format = '%m/%d/%Y')
    #Make date of the same year i.e 2016
    SampleData$date <- as.Date(format(SampleData$date, "2016-%m-%d"))
    #Get season for each date. 
    SampleData$SightSeason <- getSeason(SampleData$date)
    
    head(SampleData)
    #       SightDate       date SightSeason
    #9977  10/13/2015 2016-10-13        Fall
    #11703  6/26/2016 2016-06-26      Summer
    #15804  9/15/2017 2016-09-15      Summer
    #6177  12/21/2013 2016-12-21      Winter
    #12954  12/3/2016 2016-12-03        Fall
    #9707   9/18/2015 2016-09-18      Summer