我正在尝试将数据表中多列的数据转换为POSIXct格式。我想定义要在字符向量中转换的列。这是一个例子:
> require(data.table)
>
> # Setting up the time data.
> BD <- as.POSIXct("2015-01-01 00:00:00", tz = "UTC")
> ED <- as.POSIXct("2015-12-31 23:59:59", tz = "UTC")
> TimeSEQ <- seq(BD, ED, by = "min")
> TimeSEQ <- as.character(TimeSEQ)
>
> # Cutting the sequence up into 4 chunks.
> T1 <- TimeSEQ[1:131400]
> T2 <- TimeSEQ[131401:262800]
> T3 <- TimeSEQ[262801:394200]
> T4 <- TimeSEQ[394201:525600]
>
> # Setting up the Data Table.
> DT <- data.table(Index = 1:525600, T1 = T1, T2 = T2, T3 = T3, T4 = T4)
> print(DT)
Index T1 T2 T3 T4
1: 1 2015-01-01 00:00:00 2015-04-02 06:00:00 2015-07-02 12:00:00 2015-10-01 18:00:00
2: 2 2015-01-01 00:01:00 2015-04-02 06:01:00 2015-07-02 12:01:00 2015-10-01 18:01:00
3: 3 2015-01-01 00:02:00 2015-04-02 06:02:00 2015-07-02 12:02:00 2015-10-01 18:02:00
4: 4 2015-01-01 00:03:00 2015-04-02 06:03:00 2015-07-02 12:03:00 2015-10-01 18:03:00
5: 5 2015-01-01 00:04:00 2015-04-02 06:04:00 2015-07-02 12:04:00 2015-10-01 18:04:00
---
525596: 525596 2015-04-02 05:55:00 2015-07-02 11:55:00 2015-10-01 17:55:00 2015-12-31 23:55:00
525597: 525597 2015-04-02 05:56:00 2015-07-02 11:56:00 2015-10-01 17:56:00 2015-12-31 23:56:00
525598: 525598 2015-04-02 05:57:00 2015-07-02 11:57:00 2015-10-01 17:57:00 2015-12-31 23:57:00
525599: 525599 2015-04-02 05:58:00 2015-07-02 11:58:00 2015-10-01 17:58:00 2015-12-31 23:58:00
525600: 525600 2015-04-02 05:59:00 2015-07-02 11:59:00 2015-10-01 17:59:00 2015-12-31 23:59:00
>
> # Making the names I want to convert.
> convNames <- c("T1", "T2", "T3", "T4")
>
> #####
> # With Data Frame
> #####
>
> # Converting to Data Frame.
> DF <- as.data.frame(DT)
>
> # Converting the columns of convNames.
> class(DF$T1)
[1] "character"
> for (name in convNames) { DF[, name] <- as.POSIXct(DF[, name], tz = "UTC") }
> class(DF$T1)
[1] "POSIXct" "POSIXt"
我希望能够在数据表中进行此转换,但不知道如何动态定义列/将转换应用于每列。我还提供了一个数据框方法,以进一步显示我正在尝试做的事情。我尝试过这些方法:
> DT[, convNames[1], with = FALSE]
T1
1: 2015-01-01 00:00:00
2: 2015-01-01 00:01:00
3: 2015-01-01 00:02:00
4: 2015-01-01 00:03:00
5: 2015-01-01 00:04:00
---
525596: 2015-04-02 05:55:00
525597: 2015-04-02 05:56:00
525598: 2015-04-02 05:57:00
525599: 2015-04-02 05:58:00
525600: 2015-04-02 05:59:00
但是,这会在数据表类中返回:
> class(DT[, convNames[1], with = FALSE])
[1] "data.table" "data.frame"
我不想明确地输入列名,因为我可能需要稍后更改它们。我只是不确定如何在数据表包中执行此操作并进行了大量搜索;在哪里我要么找不到解决方案,要么找不懂解决方案。谢谢。