要像您一样转换日期时间字符串,我建议使用结构函数,如下所示:
> # begin with date-time values stored as character vectors ("strings"):
> w
[1] "2000-07-06 00:00:00"
> typeof(w)
[1] "character"
> class(w)
[1] "character"
> # use structure to convert them
> w = structure(w, class=c("POSIXt, POSIXct"))
> # verify that they are indeed R-recognizable date-time objects:
> w
[1] "2000-07-06 00:00:00"
attr(,"class")
[1] "POSIXt, POSIXct"
w/r/t the mechanics: R functions are vectorized, so you can just pass in a column of dates and bind the result to the same column, like so:
> j$day
[1] 1353967580 1353967581 1353967583 1353967584 1353967585 1353967586
[7] 1353967587 1353967588 1353967589 1353967590
> j$day = structure(day, class=c("POSIXt", "POSIXct"))
> day
[1] "2012-11-26 14:06:20 PST" "2012-11-26 14:06:21 PST" "2012-11-26 14:06:22 PST"
[4] "2012-11-26 14:06:23 PST" "2012-11-26 14:06:24 PST" "2012-11-26 14:06:25 PST"
[7] "2012-11-26 14:06:26 PST" "2012-11-26 14:06:28 PST" "2012-11-26 14:06:29 PST"
[10] "2012-11-26 14:06:30 PST"