[R] How to make a vector/list/array of POSIXlt object?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Feb 18 15:09:58 CET 2008


Your mapply creates a list so to do it that way convert result to a
POSIXct vector first.  Using your strptime2 and dd, tt & dat from mine:

dt <- do.call(c, mapply(strptime2, dd, tt, SIMPLIFY = FALSE, USE.NAMES = FALSE))
data.frame(dt, dat)

# or just
data.frame(dt = strptime2(dd, tt), dat)




On Feb 18, 2008 8:37 AM, Bo Zhou <bozhou1981 at hotmail.com> wrote:
>
> Ta. I will give that code a bash.
>
> Could you explain why my code didn't work?
>
>
>
>
>
> ________________________________
> > Date: Mon, 18 Feb 2008 00:25:44 -0500
>
>
> > From: ggrothendieck at gmail.com
> > To: bozhou1981 at hotmail.com
> > Subject: Re: [R] How to make a vector/list/array of POSIXlt object?
> > CC: r-help at r-project.org
> >
> > If the problem is that you have a vector of dates, a vector of times
> > and a vector of data and you want to create a data frame with one
> > POSIXct column and one column of data then try this:
> >
> > dd <- c("01/22/2008", "02/13/2008")
> > tt <- c("01:01:00", "23:01:12")
> > dat <- 1:2
> >
> > data.frame(dt = strptime(paste(dd, tt), "%m/%d/%Y %H:%M:%S"), dat)
> >
> > # if you don't need subsecond data or time zones you could use chron
> >
> > library(chron)
> > data.frame(dt = chron(dd, tt), dat)
> >
> > If this is intended to be a time series you might want to look at the zoo
> > package. It has three vignettes that give more info.
> >
> >
> > On Feb 17, 2008 11:54 PM, Bo Zhou <bozhou1981 at hotmail.com> wrote:
> > > Hi Gabor,
> > >
> > > I'm using this code but it doesn't work for me
> > >
> > > > strptime2<-function (date,time) as.POSIXct(strptime(paste(date,time),
> > > "%m/%d/%Y %H:%M:%S"))
> > > > dt=mapply(strptime2, "01/01/2008", "00:00:00", SIMPLIFY=FALSE,
> > > USE.NAMES=FALSE)
> > > > df=data.frame(X1=dt,X2=1)
> > > > dt
> > > [[1]]
> > > [1] "2008-01-01 Eastern Standard Time"
> > >
> > > > df
> > > structure.1199163600..class...c..POSIXt....POSIXct....tzone...... X2
> > > 1 2008-01-01 1
> > >
> > > Here df looks very wrong to me.
> > >
> > > So I tested this code:
> > >
> > > > df2=data.frame(X1=as.POSIXct(Sys.time()),X2=1)
> > > > df2
> > > X1 X2
> > > 1 2008-02-17 23:43:08 1
> > > > class(df2$X1)
> > > [1] "POSIXt" "POSIXct"
> > >
> > > Ah this worked as I expected.
> > >
> > > So some tweaking here - SIMPLIFY is set TRUE now:
> > >
> > > > strptime2<-function (date,time) as.POSIXct(strptime(paste(date,time),
> > > "%m/%d/%Y %H:%M:%S"))
> > > > dt=mapply(strptime2, "01/01/2008", "00:00:00", SIMPLIFY=TRUE,
> > > USE.NAMES=FALSE)
> > > > df=data.frame(X1=dt,X2=1)
> > > > df
> > > X1 X2
> > > 1 1199163600 1
> > > > class(df$X1)
> > > [1] "numeric"
> > >
> > > Hmm... it worked, but not in a way I wanted. The class info is missing.
> > >
> > > So how to get the result like this below? I do need that mapply +
> > > strptime(paste), cos my CSV file is formatted in that way!
> > >
> > > > df2
> > > X1 X2
> > > 1 2008-02-17 23:43:08 1
> > > > class(df2$X1)
> > > [1] "POSIXt" "POSIXct"
> > >
> > >
> > > Any insight?
> > >
> > > Cheers,
> > >
> > > Bo
> > >
> > >
> > >
> > > > Date: Sun, 17 Feb 2008 15:53:28 -0500
> > > > From: ggrothendieck at gmail.com
> > > > To: bozhou1981 at hotmail.com
> > > > Subject: Re: [R] How to make a vector/list/array of POSIXlt object?
> > > > CC: r-help at r-project.org
> > >
> > >
> > > >
> > > > Normally one uses POSIXct rather than POSIXlt for storage. See R News
> 4/1
> > > for
> > > > more info on date and time classes.
> > > >
> > > > On Feb 17, 2008 3:45 PM, Bo Zhou <bozhou1981 at hotmail.com> wrote:
> > > > >
> > > > > Hi Guys,
> > > > >
> > > > > I'm cooking up my time series code. I want a data frame with first
> > > column as timestamp in POSIXlt format.
> > > > >
> > > > > I hit on this the problem of how to create an array/list/vector of
> > > POSIXlt objects. Code is as follows
> > > > >
> > > > >
> > > > >
> > > > > > dtt=array(dim = 2)
> > > > > > t=as.POSIXlt( strptime("07/12/07 13:20:01", "%m/%d/%Y
> > > %H:%M:%S",tz="GMT"))
> > > > > > dtt
> > > > > [1] NA NA
> > > > > > t
> > > > > [1] "0007-07-12 13:20:01 GMT"
> > > > > > dtt[1]=t
> > > > > Warning message:
> > > > > In dtt[1] = t :
> > > > > number of items to replace is not a multiple of replacement length
> > > > > > class(dtt)
> > > > > [1] "list"
> > > > > > class(t)
> > > > > [1] "POSIXt" "POSIXlt"
> > > > > > unclass(t)
> > > > > $sec
> > > > > [1] 1
> > > > >
> > > > > $min
> > > > > [1] 20
> > > > >
> > > > > $hour
> > > > > [1] 13
> > > > >
> > > > > $mday
> > > > > [1] 12
> > > > >
> > > > > $mon
> > > > > [1] 6
> > > > >
> > > > > $year
> > > > > [1] -1893
> > > > >
> > > > > $wday
> > > > > [1] 4
> > > > >
> > > > > $yday
> > > > > [1] 192
> > > > >
> > > > > $isdst
> > > > > [1] 0
> > > > >
> > > > > attr(,"tzone")
> > > > > [1] "GMT"
> > > > >
> > > > >
> > > > >
> > > > > Seems like POSIXlt is matrix in this case.
> > > > >
> > > > > Any suggestions?
> > > > >
> > > > > Cheers,
> > > > >
> > > > > B
> > > > > _________________________________________________________________
> > > > > [[elided Hotmail spam]]
> > > > >
> > > > > [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > R-help at 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.
> > > > >
> > >
> > > ________________________________
> > > Need to know the score, the latest news, or you need your Hotmail(R)-get
> your
> > > "fix". Check it out.
> > >
>
> ________________________________
> Connect and share in new ways with Windows Live. Get it now!



More information about the R-help mailing list