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.
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 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.David Winsemius, MD West Hartford, CT
> 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.
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...