[R] Date problem

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 14 20:35:16 CET 2006


On 3/14/06, natalia norden <norden at cict.fr> wrote:
> Hello,
> I have some "stupid" problems managing "date" data.
> I have a colomn "date", which I converted from a character representation:
>
> for example:
> a="26/02/06"
> date=strptime(a,format="%d/%m/%y")
>
> For one part of the analysis, I'm interested only in the month and the
> year, so I did:
>
> m.y=strftime(date,format="%m/%y")
>
> This returns me "02/06", but this is an object of class "character" and
> I can't convert it into an object of class "Date". Doing
> strptime(m.y,format="%m/%y") or as.Date(m.y,format="%m/%y") returns me NA
>
> How can a convert this colomn "m.y" in a Date class?
>
> Actually, I need it to plot fruiting data against time (month and year).
> Because I have many values of seeds in a month, I used the function tapply:
> seeds=tapply(data$seed,data$m.y,sum)
> But plot(x=names(seeds),y=seeds) doesn't work. Does anyone know an
> easier way?

Are these time series?  If so, you could use the zoo package:

library(zoo)
seeds <- zoo(1:100, as.Date("2000-01-01") + 0:99) # test data
seeds.mon <- aggregate(seeds, as.Date(as.yearmon(time(seeds))), sum)
plot(seeds.mon)

or if seeds is not a time series, i.e. does not have unique dates:

library(zoo)
seeds <- 1:100
dd <- rep(as.Date("2000-01-01") + 0:49, 2)
seeds.mon <- aggregate(zoo(seeds), as.Date(as.yearmon(dd)), sum)
plot(seeds.mon)


For info:

library(zoo)
vignette("zoo")




More information about the R-help mailing list