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.

Reply via email to