On Wed, Feb 1, 2012 at 5:26 PM, Sam Albers <tonightstheni...@gmail.com> wrote: > Hello all, > > ## I am trying to convert some year and month data into a single > variable that has a date format so I can plot a proper x axis. > ## I've made a few tries at this and search around but I haven't found > anything. I am looking for something of the format "%Y-%m" > > ## A data.frame > df <- data.frame(x=rnorm(36, 1, 10), month=rep(1:12, each = 3), > year=c(2000,2001,2002)) > > ## One option. I'm not totally sure why this doesn't work > df$Date <- as.Date(paste(df$year, df$month,sep="-"), "%Y-%m") > > ## This adds the monthly total to the day rather than the MOnday and this > option > ## is messy anyway as I am adding a day to the variable > or = format(ISOdate(df$year-1, 12, 31), "%Y-%m-%d") > df$Date2 = as.Date(or) + df$month > > ## Just a plot to illustrate this. > plot(x~Date2, data=df) > > ## Any thoughts on how I can combine the year and the month into a > form that is useful for plotting? >
Try "yearmon" class in zoo: library(zoo) df$tt <- as.yearmon(paste(df$year, df$month, sep = "-")) head(df$tt) # look at it plot(x ~ tt, df) # plot it or you could create a zoo object: z <- zoo(df$x, df$tt) head(z) # look at it plot(z) # plot it -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.