categorizing date in R [closed]

社会主义新天地 提交于 2020-01-07 09:56:14

问题


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


来源:https://stackoverflow.com/questions/28662110/categorizing-date-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!