Re: [R] How to make a vector/list/array of POSIXlt object?
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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > > To: [EMAIL PROTECTED] > > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > > CC: r-help@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 <[EMAIL PROTECTED]> 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..POSIXtPOSIXcttzone.. 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: [EMAIL PROTECTED] > > > > To: [EMAIL PROTECTED] > > > > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > > > > CC: r-help@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 <[EMAIL PROTECTED]> 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 a
Re: [R] How to make a vector/list/array of POSIXlt object?
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: [EMAIL PROTECTED] > To: [EMAIL PROTECTED] > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > CC: r-help@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 <[EMAIL PROTECTED]> 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..POSIXtPOSIXcttzone.. X2 > > 12008-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: [EMAIL PROTECTED] > > > To: [EMAIL PROTECTED] > > > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > > > CC: r-help@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 <[EMAIL PROTECTED]> 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
Re: [R] How to make a vector/list/array of POSIXlt object?
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 <[EMAIL PROTECTED]> 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..POSIXtPOSIXcttzone.. X2 > 12008-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: [EMAIL PROTECTED] > > To: [EMAIL PROTECTED] > > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > > CC: r-help@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 <[EMAIL PROTECTED]> 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@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. > __ 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.
Re: [R] How to make a vector/list/array of POSIXlt object?
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..POSIXtPOSIXcttzone.. X2 12008-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: [EMAIL PROTECTED] > To: [EMAIL PROTECTED] > Subject: Re: [R] How to make a vector/list/array of POSIXlt object? > CC: r-help@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 <[EMAIL PROTECTED]> 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@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Ā®-get your "fix". [[alternative HTML version deleted]] __ 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.
Re: [R] How to make a vector/list/array of POSIXlt object?
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 <[EMAIL PROTECTED]> 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@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. > __ 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.
[R] How to make a vector/list/array of POSIXlt object?
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@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.