在日期上使用R剪切功能 [英] Using R cut function on dates

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

问题描述

我有一个数据框来提供体育赛事的出勤率

I have a dataframe giving attendances at sports events

Crowd    matchDate
2345      1993-01-26
4567      1993-08-01
8888      1994-03-02
1298      1994-11-07
9876      1995-09-01 etc

1237      2011-09-09

比赛日期是POSIXct类

The matchdate is a POSIXct class

我希望能够基于日期创建季节因子,这样每个季节都从8月1日到7月31日运行,例如1992/3因子将包括1992-08-01至1993-07-31日期

I want to be able to create a season factor based on the date such that each season runs from, say, 1st August to 31 July e.g factor 1992/3 would include dates 1992-08-01 to 1993-07-31

理想情况下,它将是我可以申请多次分析的功能,而不必在一年中的开始日期和结束日期相同

ideally it would be a function that I could apply for several analyses, not necessarily with same start and end dates in the year

推荐答案

如果您的问题与如何自动生成中断和标签更相关,也许会有所帮助

If your question is more related to how you automatically generate the breaks and labels, maybe this will help

DF <- data.frame(matchDate = as.POSIXct(as.Date(sample(5000,100,replace=TRUE), origin="1993-01-01")))

years <- 1992:2011
DF$season <- cut(DF$matchDate, 
  breaks=as.POSIXct(paste(years,"-08-01",sep="")),
  labels=paste(years[-length(years)],years[-length(years)]+1,sep="/"))

这篇关于在日期上使用R剪切功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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