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

From: Bo Zhou <bozhou1981_at_hotmail.com>
Date: Mon, 18 Feb 2008 13:37:29 +0000

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.
> >


08

        [[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. Received on Mon 18 Feb 2008 - 13:51:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Feb 2008 - 14:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive