Using R cut function on dates

后端 未结 2 1858
天涯浪人
天涯浪人 2020-12-31 03:49

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         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 04:06

    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="/"))
    

提交回复
热议问题