将原始数据转换为R日期/时间类

将原始数据转换为R日期/时间类,r,date,datetime,posixct,R,Date,Datetime,Posixct,我已经成功地抓取了一堆我现在想在R中操作的表。我对R是新手,但这似乎是一个很好的解决方法 我当前将数据存储在一个以条形分隔的CSV中,看起来如下所示: FIRST|LAST|9812036311|string-string|1999-07-06 00:00:00|2000-07-06 00:00:00|12345|1999-07-27 00:00:00|2,518.50 我可以通过以下方式阅读: j <- read.table('my_data.csv', header = FALSE,

我已经成功地抓取了一堆我现在想在R中操作的表。我对R是新手,但这似乎是一个很好的解决方法

我当前将数据存储在一个以条形分隔的CSV中,看起来如下所示:

FIRST|LAST|9812036311|string-string|1999-07-06 00:00:00|2000-07-06 00:00:00|12345|1999-07-27 00:00:00|2,518.50
我可以通过以下方式阅读:

j <- read.table('my_data.csv', header = FALSE, sep = "|")

jCheckout
colclasses
,以帮助R了解CSV的每列中应包含的数据类型。另外,请查看处理日期格式的软件包。

要像您一样转换日期时间字符串,我建议使用structure函数,如下所示:

> # 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"
> 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"
w/r/t力学:r函数是矢量化的,因此您只需传入一列日期并将结果绑定到同一列,如下所示:

> # 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"
> 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"

事实证明,我只需要
as.Date
——作为一个健全性检查,我用修改后的日期创建了一个新列

j$better.date <- as.Date(j$original.date, format="%Y-%d-%m")

j$better.date通知OP使用as.POSIXct可能会有帮助,因为将来的转换可能与默认格式不完全匹配。我只需要as.date,我想:
j[[6]]