1

我已经成功地抓取了一堆我现在想在 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 = "|")

但是......如何将这些日期列转换为 R 可以读取的日期?

我需要先定义一个框架吗?

4

3 回答 3

2

按照此处colclasses的建议进行结帐,以帮助 R 了解它应该在 CSV 的每一列中预期的数据类型。还要查看处理日期格式的lubridate包。

于 2012-11-26T21:56:55.527 回答
1

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

> # 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"
于 2012-11-26T22:07:13.887 回答
0

It turns out I just needed as.Date -- as a sanity check, I created a new column with the modified date.

j$better.date <- as.Date(j$original.date, format="%Y-%d-%m")
于 2013-01-16T16:23:54.003 回答