Re: [R] Lost in POSIX
Dimitri Shvorob wrote: df = structure(list(t = structure(c(1033963406.044, 1033974144.847, + 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, + 3L), class = data.frame) df$min = trunc(df$t,units=mins) does not work, Jeff; you will see that my original post suggests familiarity with 'trunc' :) Well, perhaps you should read the error message or the Value section of ?trunc.POSIXt, and convert the result to a compact type... df$min - trunc( df$t, units=mins ) Error in `$-.data.frame`(`*tmp*`, min, value = list(sec = 0, min = c(3L, : replacement has 9 rows, data has 3 df$min - as.POSIXct( trunc( df$t, units=mins ) ) str(df) 'data.frame': 3 obs. of 2 variables: $ t : POSIXct, format: 2002-10-06 21:03:26 2002-10-07 00:02:24 ... $ min: POSIXct, format: 2002-10-06 21:03:00 2002-10-07 00:02:00 ... -- --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k __ 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] Lost in POSIX
Dimitri Shvorob wrote: .. One issue with the solution proposed by Jeff is that the transformed column does not have the original's type: x = structure(list(time = structure(c(1020232904.818, 1020232904.818 ), class = c(POSIXt, POSIXct), tzone = ), price = c(321, 323.5), minute = c(1020232860, 1020232860)), .Names = c(time, price, minute), row.names = 1:2, class = data.frame) minute - function(t) { d - as.POSIXlt(t, origin = as.Date(1970-01-01)) d$sec - 0 as.POSIXct(d) } x$minute = sapply(x$time, minute) head(x) time price minute 1 2002-05-01 07:01:44 321.0 1020232860 2 2002-05-01 07:01:44 323.5 1020232860 class(x.l$minute) [1] numeric That is not an issue with the minute function, as you can see if you evaluate minute(x$time) [1] 2002-04-30 23:01:00 PDT 2002-04-30 23:01:00 PDT or str(minute(x$time)) POSIXct[1:2], format: 2002-04-30 23:01:00 2002-04-30 23:01:00 rather, you are seeing a side effect of sapply. -- --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k __ 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] Lost in POSIX
On Dec 25, 2010, at 2:25 PM, Dimitri Shvorob wrote: df = structure(list(t = structure(c(1033963406.044, 1033974144.847, + 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, + 3L), class = data.frame) df$min = trunc(df$t,units=mins) does not work, ??? seems to work on my system. Perhaps you should say what you mean by not work df t min 1 2002-10-07 00:03:26 2002-10-07 00:03:00 2 2002-10-07 03:02:24 2002-10-07 03:02:00 3 2002-10-07 07:00:18 2002-10-07 07:00:00 sessionInfo() R version 2.12.1 (2010-12-16) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] grid splines stats graphics grDevices utils datasets methods base other attached packages: [1] nlme_3.1-97lme4_0.999375-37 Matrix_0.999375-46 zoo_1.6-4 ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.2.1 MASS_7.3-9 [10] rms_3.1-0 Hmisc_3.8-3survival_2.36-2 sos_1.3-0 brew_1.0-4 lattice_0.19-13 loaded via a namespace (and not attached): [1] cluster_1.13.2 stats4_2.12.1 tools_2.12.1 Jeff; you will see that my original post suggests familiarity with 'trunc' :) -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3163914.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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] Lost in POSIX
df = structure(list(t = structure(c(1033963406.044, 1033974144.847, + 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, + 3L), class = data.frame) df$min = trunc(df$t,units=mins) does not work, Jeff; you will see that my original post suggests familiarity with 'trunc' :) -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3163914.html Sent from the R help mailing list archive at Nabble.com. __ 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] Lost in POSIX
.. One issue with the solution proposed by Jeff is that the transformed column does not have the original's type: x = structure(list(time = structure(c(1020232904.818, 1020232904.818 ), class = c(POSIXt, POSIXct), tzone = ), price = c(321, 323.5), minute = c(1020232860, 1020232860)), .Names = c(time, price, minute), row.names = 1:2, class = data.frame) minute - function(t) { d - as.POSIXlt(t, origin = as.Date(1970-01-01)) d$sec - 0 as.POSIXct(d) } x$minute = sapply(x$time, minute) head(x) time price minute 1 2002-05-01 07:01:44 321.0 1020232860 2 2002-05-01 07:01:44 323.5 1020232860 class(x.l$minute) [1] numeric -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3163969.html Sent from the R help mailing list archive at Nabble.com. __ 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] Lost in POSIX
Dimitri Shvorob wrote: Nor would I call this much of an improvement in clarity... what about min? You want to know the minimum? LOL. (And apologies for the insensitivity). Thank you for help, Jeff. This works, but I am still curious to see a solution based on trunc, if anyone can find it. You mean like trunc(df$t,units=mins) ? See ?trunc.POSIXt for hints on arguments to units parameter... __ 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] Lost in POSIX
df$dt - as.Date(df$t) Thank you, David, but I need a *time* value. day was a confusing special case; how about min? -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3053146.html Sent from the R help mailing list archive at Nabble.com. __ 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] Lost in POSIX
Dimitri Shvorob wrote: df$dt - as.Date(df$t) Thank you, David, but I need a *time* value. day was a confusing special case; how about min? Your original question was NOT clear on this point... it was not David's fault your question was confusing, and you haven't even hinted at an apology for leading him down the wrong path. Nor would I call this much of an improvement in clarity... what about min? You want to know the minimum? No? You want to truncate to minute? Why then did your original Try 2 attempt to zero out the minute (truncate to hour)? Perhaps the following will be enough help you figure out an answer to whatever your question is. If it doesn't, try asking again with consistent variable naming and sample results you want to obtain. truncMinute - function(dtm) { d - as.POSIXlt(dtm) d$sec - 0 as.POSIXct(d) } df$tt - truncMinute(df$t) __ 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] Lost in POSIX
Nor would I call this much of an improvement in clarity... what about min? You want to know the minimum? LOL. (And apologies for the insensitivity). Thank you for help, Jeff. This works, but I am still curious to see a solution based on trunc, if anyone can find it. -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3053329.html Sent from the R help mailing list archive at Nabble.com. __ 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] Lost in POSIX
Can someone please fix this snippet? (i.e. append to the dataframe a column containing truncated time value)? df = structure(list(t = structure(c(1033963406.044, 1033974144.847, 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, 3L), class = data.frame) # Try 1 df$day = trunc.POSIXt(as.POSIXlt(df$t, origin = 1970-01-01), units = day) Error in `$-.data.frame`(`*tmp*`, day, value = list(0, 0L, 0L, 7L, : replacement has 9 rows, data has 3 # Try 2 f = function(t) trunc.POSIXt(as.POSIXlt(t, origin = 1970-01-01), units = day) df$day = sapply(df$t, f) Error in `$-.data.frame`(`*tmp*`, day, value = list(sec = 0, min = 0L, : replacement has 9 rows, data has 3 -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3052768.html Sent from the R help mailing list archive at Nabble.com. __ 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] Lost in POSIX
On Nov 21, 2010, at 3:52 PM, Dimitri Shvorob wrote: Can someone please fix this snippet? (i.e. append to the dataframe a column containing truncated time value)? df = structure(list(t = structure(c(1033963406.044, 1033974144.847, 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, 3L), class = data.frame) df t 1 2002-10-07 00:03:26 2 2002-10-07 03:02:24 3 2002-10-07 07:00:18 df$dt - as.Date(df$t) df t dt 1 2002-10-07 00:03:26 2002-10-07 2 2002-10-07 03:02:24 2002-10-07 3 2002-10-07 07:00:18 2002-10-07 # Try 1 df$day = trunc.POSIXt(as.POSIXlt(df$t, origin = 1970-01-01), units = day) Error in `$-.data.frame`(`*tmp*`, day, value = list(0, 0L, 0L, 7L, : replacement has 9 rows, data has 3 # Try 2 f = function(t) trunc.POSIXt(as.POSIXlt(t, origin = 1970-01-01), units = day) df$day = sapply(df$t, f) Error in `$-.data.frame`(`*tmp*`, day, value = list(sec = 0, min = 0L, : replacement has 9 rows, data has 3 -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3052768.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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.