Question

I have a dataset with a date column. I am trying to take that date column and transform it to one that only has month and year, as a Posix date. The reason I need to be able to do this is to be able to use a bin that is a month wide in ggplot2 and scale_x_date. Here is a test dataset:

testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06","2013-07-03","2013-08-04","2013-09-05","2013-10-06","2013-11-07")), plant = LETTERS[1:5], PlantID = c(1,2,3,4,5,1,2,3,6,7), product = as.factor(letters[26:22]), rating = runif(30))

Basically, I want to turn the date column into one that is just year-month.

Any quick, eloquent ways to do such a thing?

EDIT: I created another thread that has more of the end goal of what I'm looking for. It is located here: Creating month-sized bins with ggplot2 in r

Was it helpful?

Solution

From the manual:

"The "yearmon" class is used to represent monthly data. Internally it holds the data as year plus 0 for January, 1/12 for February, 2/12 for March and so on in order that its internal representa- tion is the same as ts class with frequency = 12."

> require(zoo)
> as.yearmon("2007-03-01")
[1] "Mar 2007"
> as.numeric(as.yearmon("2007-03-01"))
[1] 2007.167

Is this what you really want, a yearmon which is a fraction of a year? If this works for you see

http://cran.r-project.org/web/packages/zoo/zoo.pdf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top