-4

I'm working with a dataset in R where the main area of interest is the date. (It has to do with army skirmishes and the date of the skirmish is recorded). I wanted to check if these were more likely to happen in a given season, or near a holiday, etc, so I want to be able to see how many dates there are in the summer, winter, etc but I'm sort of at a loss for how to do that.

deeno
  • 11
  • 1

1 Answers1

1

A general recommendation: use the package lubridate for converting from strings to dates if you're having trouble with that. use cut() to divide dates into ranges, like so:

someDates <- c( '1-1-2013',
               '2-14-2013',
               '3-5-2013',
               '8-21-2013',
               '9-15-2013',
               '11-28-2013',
               '12-22-2013')
cutpoints<- c('1-1-2013',# star of range 'winter'
              '3-20-2013',# spring
              '6-21-2013',# summer
              '9-23-2013',# fall
              '12-21-2013',# winter
              '1-1-2014')# end of range

library(lubridate)
temp <- cut(mdy(someDates),
            mdy(cutpoints),
            labels=FALSE)
someSeasons  <-  c('winter',
                   'spring',
                   'summer',
                   'fall',
                   'winter')[temp]

Now use 'someSeasons' to group your data into date ranges with your favorite statistical analysis. For a choice of statistical analysis, poisson regression adjusting for exposure (i.e. length of the season), comes to mind, but that is probably a better question for Cross Validated

You can make a vector of cut points with regular intervals like so:

cutpoints<- c('3-20-2013',# spring
              '6-21-2013',# summer
              '9-23-2013',# fall
              '12-21-2013')# winter

temp <- cut(mdy(someDates),
            outer(mdy(cutpoints), years(1:5),`+`),
            labels=F)
someSeasons  <-  c('spring',
                   'summer',
                   'fall',
                   'winter')[(temp-1)%% 4 + 1] #the index is just a little tricky...
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
  • how would I do this if I have a number of years though? would leaving off the year work, or no? – deeno Feb 22 '15 at 20:01
  • See edits with the regular sequence, and see `?lubridate' for constructing regular sequences of dates. – Jthorpe Feb 22 '15 at 20:57
  • I have asked a question on how to assign a season to a date. Have you seen the proposed solutions? http://stackoverflow.com/questions/9500114/find-which-season-a-particular-date-belongs-to – Roman Luštrik Feb 23 '15 at 09:03