I was trying to convert a vector of POSIXct into a list of POSIXct, However, I had a problem that I wanted to share with you. Works fine with, say, numeric:> v = c(1, 2, 3) > v[1] 1 2 3> str(v)?num [1:3] 1 2 3> l = as.vector(v, mode="list") > l[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3> str(l)List of 3 ?$ : num 1 ?$ : num 2 ?$ : num 3 If you try it with POSIXct, on the other hand?> v = c(Sys.time(), Sys.time()) > v[1] "2013-05-20 18:02:07 BRT" "2013-05-20 18:02:07 BRT"> str(v)?POSIXct[1:2], format: "2013-05-20 18:02:07" "2013-05-20 18:02:07"> l = as.vector(v, mode="list") > l[[1]] [1] 1369083728 [[2]] [1] 1369083728> str(l)List of 2 ?$ : num 1.37e+09 ?$ : num 1.37e+09 The POSIXct values are coerced to numeric, which is unexpected. The documentation for as.vector says:?"The default method handles 24 input types and 12 values of?type: the details of most coercions are undocumented and subject to change."?It would appear that treatment for POSIXct is either missing or needs adjustment. Unlist (for the reverse) is documented to converting to base types, so I can't complain. Just wanted to share that I ended up giving up on vectorization and writing the two following functions: unlistPOSIXct <- function(x) { ? retval = rep(Sys.time(), length(x)) ? for (i in 1:length(x)) retval[i] = x[[i]] ? return(retval) } listPOSIXct <- function(x) { ? retval = list() ? for (i in 1:length(x)) retval[[i]] = x[i] ? return(retval) } Is there a better way to do this (other than using *apply instead of for above) that better leverages vectorization? Am I missing something here? Thanks! --? Alexandre Sieira CISA, CISSP, ISO 27001 Lead Auditor "The truth is rarely pure and never simple." Oscar Wilde, The Importance of Being Earnest, 1895, Act I
Try using as.list(x) instead of as.vector(x, mode="list"). The former has a method for POSIXct; the latter does not. > x <- as.POSIXct(c("2013-05-20 14:28", "2013-11-30 22:10"), tz="US/Pacific") > x [1] "2013-05-20 14:28:00 PDT" "2013-11-30 22:10:00 PST" > as.list(x) [[1]] [1] "2013-05-20 14:28:00 PDT" [[2]] [1] "2013-11-30 22:10:00 PST" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Alexandre Sieira > Sent: Monday, May 20, 2013 2:10 PM > To: r-help at r-project.org > Subject: [R] as.vector with mode="list" and POSIXct > > I was trying to convert a vector of POSIXct into a list of POSIXct, However, I had a > problem that I wanted to share with you. > > Works fine with, say, numeric: > > > > v = c(1, 2, 3) > > v > [1] 1 2 3 > > str(v) > ?num [1:3] 1 2 3 > > l = as.vector(v, mode="list") > > l > [[1]] > [1] 1 > > [[2]] > [1] 2 > > [[3]] > [1] 3 > > > str(l) > List of 3 > ?$ : num 1 > ?$ : num 2 > ?$ : num 3 > > If you try it with POSIXct, on the other hand? > > > > v = c(Sys.time(), Sys.time()) > > v > [1] "2013-05-20 18:02:07 BRT" "2013-05-20 18:02:07 BRT" > > str(v) > ?POSIXct[1:2], format: "2013-05-20 18:02:07" "2013-05-20 18:02:07" > > l = as.vector(v, mode="list") > > l > [[1]] > [1] 1369083728 > > [[2]] > [1] 1369083728 > > > str(l) > List of 2 > ?$ : num 1.37e+09 > ?$ : num 1.37e+09 > > The POSIXct values are coerced to numeric, which is unexpected. > > The documentation for as.vector says:?"The default method handles 24 input types and > 12 values of?type: the details of most coercions are undocumented and subject to > change."?It would appear that treatment for POSIXct is either missing or needs > adjustment. > > Unlist (for the reverse) is documented to converting to base types, so I can't complain. > Just wanted to share that I ended up giving up on vectorization and writing the two > following functions: > > > unlistPOSIXct <- function(x) { > ? retval = rep(Sys.time(), length(x)) > ? for (i in 1:length(x)) retval[i] = x[[i]] > ? return(retval) > } > > listPOSIXct <- function(x) { > ? retval = list() > ? for (i in 1:length(x)) retval[[i]] = x[i] > ? return(retval) > } > > Is there a better way to do this (other than using *apply instead of for above) that better > leverages vectorization? Am I missing something here? > > Thanks! > > > > > -- > Alexandre Sieira > CISA, CISSP, ISO 27001 Lead Auditor > > "The truth is rarely pure and never simple." > Oscar Wilde, The Importance of Being Earnest, 1895, Act I
I don't know what you plan to do with this list, but lists are quite a bit less efficient than fixed-mode vectors, so you are likely losing a lot of computational speed by using this list. I don't hesitate to use simple data frames (lists of vectors), but processing lists is on par with for loops, not vectorized computation. It may still support a simpler model of computation, but that is an analyst comprehension benefit rather than a computational efficiency benefit. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at 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. Alexandre Sieira <alexandre.sieira at gmail.com> wrote:>I was trying to convert a vector of POSIXct into a list of POSIXct, >However, I had a problem that I wanted to share with you. > >Works fine with, say, numeric: > > >> v = c(1, 2, 3) >> v >[1] 1 2 3 >> str(v) >?num [1:3] 1 2 3 >> l = as.vector(v, mode="list") >> l >[[1]] >[1] 1 > >[[2]] >[1] 2 > >[[3]] >[1] 3 > >> str(l) >List of 3 >?$ : num 1 >?$ : num 2 >?$ : num 3 > >If you try it with POSIXct, on the other hand? > > >> v = c(Sys.time(), Sys.time()) >> v >[1] "2013-05-20 18:02:07 BRT" "2013-05-20 18:02:07 BRT" >> str(v) >?POSIXct[1:2], format: "2013-05-20 18:02:07" "2013-05-20 18:02:07" >> l = as.vector(v, mode="list") >> l >[[1]] >[1] 1369083728 > >[[2]] >[1] 1369083728 > >> str(l) >List of 2 >?$ : num 1.37e+09 >?$ : num 1.37e+09 > >The POSIXct values are coerced to numeric, which is unexpected. > >The documentation for as.vector says:?"The default method handles 24 >input types and 12 values of?type: the details of most coercions are >undocumented and subject to change."?It would appear that treatment for >POSIXct is either missing or needs adjustment. > >Unlist (for the reverse) is documented to converting to base types, so >I can't complain. Just wanted to share that I ended up giving up on >vectorization and writing the two following functions: > > >unlistPOSIXct <- function(x) { >? retval = rep(Sys.time(), length(x)) >? for (i in 1:length(x)) retval[i] = x[[i]] >? return(retval) >} > >listPOSIXct <- function(x) { >? retval = list() >? for (i in 1:length(x)) retval[[i]] = x[i] >? return(retval) >} > >Is there a better way to do this (other than using *apply instead of >for above) that better leverages vectorization? Am I missing something >here? > >Thanks! > > > > >--? >Alexandre Sieira >CISA, CISSP, ISO 27001 Lead Auditor > >"The truth is rarely pure and never simple." >Oscar Wilde, The Importance of Being Earnest, 1895, Act I > >------------------------------------------------------------------------ > >______________________________________________ >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.
On May 20, 2013, at 2:09 PM, Alexandre Sieira wrote:> I was trying to convert a vector of POSIXct into a list of POSIXct, However, I had a problem that I wanted to share with you. > > Works fine with, say, numeric: > > >> v = c(1, 2, 3) >> v > [1] 1 2 3 >> str(v) > num [1:3] 1 2 3 >> l = as.vector(v, mode="list") >> l > [[1]] > [1] 1 > > [[2]] > [1] 2 > > [[3]] > [1] 3 > >> str(l) > List of 3 > $ : num 1 > $ : num 2 > $ : num 3 > > If you try it with POSIXct, on the other hand? > > >> v = c(Sys.time(), Sys.time()) >> v > [1] "2013-05-20 18:02:07 BRT" "2013-05-20 18:02:07 BRT" >> str(v) > POSIXct[1:2], format: "2013-05-20 18:02:07" "2013-05-20 18:02:07" >> l = as.vector(v, mode="list") >> l > [[1]] > [1] 1369083728 > > [[2]] > [1] 1369083728 > >> str(l) > List of 2 > $ : num 1.37e+09 > $ : num 1.37e+09 > > The POSIXct values are coerced to numeric, which is unexpected.You may not have expected it, but that result is what is described: From the help page for `as.vector`: "For as.vector, a vector (atomic or of type list). All attributes are removed from the result if it is of an atomic mode, ..." And since POSIXct vectors _are_ of atomic mode, all of their attributes are removed.> is.atomic(as.POSIXct("2001-01-01") )[1] TRUE (I realize that this is a belated response, and that you and Jeff Newmiller are continuing a correspondence on what I think appears to be a separate concern) , but I needed to go back to the first posting to figure out what the questions were.> > The documentation for as.vector says: "The default method handles 24 input types and 12 values of type: the details of most coercions are undocumented and subject to change." It would appear that treatment for POSIXct is either missing or needs adjustment.As in the case of factor-classed vectors, POSIXct-vectors are atomic mode. Yours was a selective reading. (And you found a way around this with `as.list`.)> is.atomic(factor("2001-01-01") )[1] TRUE -- David.> > Unlist (for the reverse) is documented to converting to base types, so I can't complain. Just wanted to share that I ended up giving up on vectorization and writing the two following functions: > > > unlistPOSIXct <- function(x) { > retval = rep(Sys.time(), length(x)) > for (i in 1:length(x)) retval[i] = x[[i]] > return(retval) > } > > listPOSIXct <- function(x) { > retval = list() > for (i in 1:length(x)) retval[[i]] = x[i] > return(retval) > } > > Is there a better way to do this (other than using *apply instead of for above) that better leverages vectorization? Am I missing something here? > > Thanks! > >-- David Winsemius Alameda, CA, USA