dataframe - Trying to map timestamps to dates in R data frame -
i have data frame df
column time
contains timestamps , function from.timestamp
from.timestamp = function(timestamp) { return(as.posixlt(timestamp/1e3, origin="1970-01-01 00:00:00 gmt")) }
that converts timestamp date type. how modify data frame timestamps converted dates?
my approach
i'm trying following:
df$time <- apply(as.array(df$time), margin=1, fun=from.timestamp)
the resulting column entries time
not date, rather sequence of numbers, e.g., 1.034, 59.000, 23.000, 7.000, 5.000, 113.000, 5.000, 157.000, 1.000
.
dput(df)
gives:
structure(list(time = c(1370674741034, 1372671995085, 1370847555008, 1371456058556, 1372570911379, 1371937835807)), .names = "time", row.names = 8831395:8831400, class = "data.frame")
just do
df$time <- from.timestamp(df$time)
the reason you're getting strange behaviour because posixlt
objects lists, components year, month, day, etc. in code, apply
receives from.timestamp
sequence of lists, combines single list. in doing so, strips class information result, why saw odd numbers. these odd numbers individual components of posixlt object representing datetime: seconds, minutes, hour, day of month, month, year, weekday, day in year.
Comments
Post a Comment