Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using R cut function on dates

Tags:

date

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

The matchdate is a POSIXct class

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

like image 845
pssguy Avatar asked Sep 06 '11 19:09

pssguy


People also ask

What does cut () do in R?

The cut function in R allows you to cut data into bins and specify 'cut labels', so it is very useful to create a factor from a continuous variable.

What is cut function?

The cut command removes the selected data from its original position, while the copy command creates a duplicate; in both cases the selected data is kept in temporary storage (the clipboard). The data from the clipboard is later inserted wherever a paste command is issued.


1 Answers

An example of my comment.

x <- as.Date(1:1000, origin = "2000-01-01")
x <- cut(x, breaks = "quarter") 

And then relabel as you please, if necessary.

labs <- paste(substr(levels(x),1,4), "/", 1:4, sep="")
x <- factor(x, labels = labs)

?cut.POSIXct

breaks
a vector of cut points or number giving the number of intervals which x is to be cut into or an interval specification, one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year", optionally preceded by an integer and a space, or followed by "s". (For "Date" objects only interval specifications using "day", "week", "month", "quarter" and "year" are allowed.)

like image 176
Brandon Bertelsen Avatar answered Oct 05 '22 23:10

Brandon Bertelsen