简体   繁体   English

将日期转换为季节

[英]Convert dates to season year

How can I change a specific range of dates to the games' season? 如何更改游戏季节的特定日期范围? For illustration: my dataframe d$Date has dates between '08/08/15' and '05/05/16', however I want to change all those dates to the specific season '2015/16'. 例如:我的数据帧d $ Date的日期介于'08 / 08/15'和'05 / 05/16'之间,但我想将所有这些日期更改为特定的季节'2015/16'。 Furthermore the dates between '08/08/14' and '05/05/15' should be '2014/15' What line of code could accomplish this? 此外,'08 / 08/14'和'05 / 05/15'之间的日期应该是'2014/15'什么样的代码可以实现这一目标?

Try this... and let me know if it help? 试试这个......让我知道它是否有帮助?

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

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

lubridate package is your friend: lubridate包是你的朋友:

library(lubridate)
date <- c("08/08/15", "08/09/16")

# extract year information 
year <- year(mdy(date))
# paste season 
season <- paste0(year, "/", year + 1)
season

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM