在R中分类日期 [英] categorizing date in R

查看:262
本文介绍了在R中分类日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R中的一个数据集,其中主要的兴趣领域是日期。 (它与军队的小规模冲突有关,并且记录了冲突的日期)。我想检查一些季节,假期等附近是否更有可能发生,所以我希望能够看到夏季,冬季等几个日期。失去了如何做到这一点。

解决方案

一般建议:使用包 lubridate 从字符串转换如果你遇到麻烦的话。使用 cut()将日期分隔为范围,如下所示:

 一些日期<  -  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',#冬季的星级
'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]

现在使用someSeasons将您的数据分组到日期范围,并使用您喜欢的
统计分析。对于统计分析的选择,poisson
回归调整曝光(即季节的长度),达到
的心态,但这可能是一个更好的问题,



您可以按照定期间隔制作剪切点矢量:

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

temp< - cut(mdy(someDates),
outer(mdy ),年(1:5),`+`),
labels = F)
someSeasons< - c('spring',
'summer',
'fall ',
'winter')[(temp-1)%% 4 + 1]#指数只是有点棘手...


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.

解决方案

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

这篇关于在R中分类日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆