-->

categorizing date in R [closed]

2019-09-22 08:38发布

问题:

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.

回答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...