Dear all, I am working with a list of objects each of which contains two POSIXct objects (say, $Start and $End) and a number of different data in addition to that. Now an easy way to extract Start times of all object could be sapply(x, "[", "Start") but this converts them all to numeric, and so does sapply(x, "[[", "Start"). lapply preserves the class but is obviously a list rather than an atomic vector but using unlist is a way of getting rid of the class again. I know the vector can be converted back using as.POSIXct and that one can set the class attribute back to "POSIXct" but is there an easier trick that I can't just figure out? Thanks in advance for any suggestions. A reproducible example: foo <- as.list(Sys.time()+1:10) sapply(foo, "[", 1) I've also noticed that trunc.POSIXt does something unexpected (to me) in some cases: foo <- as.POSIXct("2011-03-25 21:03:20")+1:10 length(foo) ## obviously 10 length(trunc(foo, "day")) # 1! This makes it impossible to use [ and length() with trunc'ed POSIXct vectors (and it is a little unexpected too that the result of using trunc with a POSIX_ct_ object is a POSIX_lt_ object) but the cure seems easy: in the code of [.POSIXt, ## old ## new x$sec <- 0 ## x$sec[] <- 0 x$min <- 0L ## x$min[] <- 0L x$hour <- 0L ## x$hour[] <- 0L After these changes, [ and length would work "correctly" (in my possibly naive understanding), and length(trunc(foo, "day")) would return 10 in the example above. Best regards, Kenn Kenn Konstabel National Institute for Health Development Hiiu 42 Tallinn Estonia
On Fri, Mar 25, 2011 at 09:14:50PM +0000, Kenn Konstabel wrote:> Dear all, > > I am working with a list of objects each of which contains two POSIXct > objects (say, $Start and $End) and a number of different data in > addition to that. Now an easy way to extract Start times of all object > could be sapply(x, "[", "Start") but this converts them all to > numeric, and so does sapply(x, "[[", "Start"). lapply preserves the > class but is obviously a list rather than an atomic vector but using > unlist is a way of getting rid of the class again. I know the vector > can be converted back using as.POSIXct and that one can set the class > attribute back to "POSIXct" but is there an easier trick that I can't > just figure out? Thanks in advance for any suggestions. > ...As Gabor Grothendieck pointed out to me when I tried to read a value as numeric, then force it to POSIXct: | This line in the code above produces an invalid object: | class(df$time) <- "POSIXct" | | It should be: | class(df$time) <- c("POSIXct", "POSIXt") Peace, david -- David H. Wolfskill r at catwhisker.org Depriving a girl or boy of an opportunity for education is evil. See http://www.catwhisker.org/~david/publickey.gpg for my public key. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 196 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110325/1095fafa/attachment.bin>
library(plyr) ldply(x,function(v){data.frame(Start=v$Start,End=v$End)}) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Kenn Konstabel <lebatsnok@gmail.com> wrote: Dear all, I am working with a list of objects each of which contains two POSIXct objects (say, $Start and $End) and a number of different data in addition to that. Now an easy way to extract Start times of all object could be sapply(x, "[", "Start") but this converts them all to numeric, and so does sapply(x, "[[", "Start"). lapply preserves the class but is obviously a list rather than an atomic vector but using unlist is a way of getting rid of the class again. I know the vector can be converted back using as.POSIXct and that one can set the class attribute back to "POSIXct" but is there an easier trick that I can't just figure out? Thanks in advance for any suggestions. A reproducible example: foo <- as.list(Sys.time()+1:10) sapply(foo, "[", 1) I've also noticed that trunc.POSIXt does something unexpected (to me) in some cases: foo <- as.POSIXct("2011-03-25 21:03:20")+1:10 length(foo) ## obviously 10 length(trunc(foo, "day")) # 1! This makes it impossible to use [ and leng th() with trunc'ed POSIXct vectors (and it is a little unexpected too that the result of using trunc with a POSIX_ct_ object is a POSIX_lt_ object) but the cure seems easy: in the code of [.POSIXt, ## old ## new x$sec <- 0 ## x$sec[] <- 0 x$min <- 0L ## x$min[] <- 0L x$hour <- 0L ## x$hour[] <- 0L After these changes, [ and length would work "correctly" (in my possibly naive understanding), and length(trunc(foo, "day")) would return 10 in the example above. Best regards, Kenn Kenn Konstabel National Institute for Health Development Hiiu 42 Tallinn Estonia_____________________________________________ 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. [[alternative HTML version deleted]]