使用 strptime 在数据表中创建新列的问题

Issues with creating a new column in datatable using strptime

我有一些数据是字符 class:

class(solarX[,MEASDATE])
[1] "character"

看起来像:

    head(solarX[,MEASDATE])
[1] "1/05/2015 0:00" "1/05/2015 0:30" "1/05/2015 1:00" "1/05/2015 1:30" "1/05/2015 2:00" "1/05/2015 2:30"

但是,我不想要它的字符格式,并且需要能够访问日期时间的小时和分钟部分。

我试过的是:

strptime(solarX[,MEASDATE], "%d/%m/%Y %H:%M"))

效果很好,它给了我:

 [1] "2015-05-01 00:00:00 AEST" "2015-05-01 00:30:00 AEST" "2015-05-01 01:00:00 AEST" "2015-05-01 01:30:00 AEST" "2015-05-01 02:00:00 AEST"
[6] "2015-05-01 02:30:00 AEST"



 class(strptime(solarX[,MEASDATE], "%d/%m/%Y %H:%M"))
[1] "POSIXlt" "POSIXt"

但是,当我通过以下方式扩充原始数据时 table:

solarX[, date := strptime(solarX[,MEASDATE], "%d/%m/%Y %H:%M")]

(solarX是我的数据table)

我收到以下警告:

    Warning message:
In `[.data.table`(solarX, , `:=`(date, strptime(solarX[, MEASDATE],  :
  Supplied 11 items to be assigned to 17568 items of column 'date' (recycled leaving remainder of 1 items)

返回的数据 table 看起来很糟糕:

    MEASDATE      rrp exp_kwh                     date
1: 1/05/2015 0:00 33.99299       0             0,0,0,0,0,0,
2: 1/05/2015 0:30 31.53335       0        0,30, 0,30, 0,30,
3: 1/05/2015 1:00 29.37092       0             0,0,1,1,2,2,
4: 1/05/2015 1:30 28.03197       0             1,1,1,1,1,1,
5: 1/05/2015 2:00 26.82800       0             4,4,4,4,4,4,
6: 1/05/2015 2:30 25.22149       0 115,115,115,115,115,115,

显然我不希望 "date" 列看起来像那样,而是用我从原始 strptime 函数获得的值填充它。

您不能在 data.table 中使用 POSIXlt,请改用 POSIXct

library(data.table)
## setting up the data
solarX <- fread('MEASDATE      rrp exp_kwh  
"1/05/2015 0:00" 33.99299 0
"1/05/2015 0:30" 31.53335 0
"1/05/2015 1:00" 29.37092 0
"1/05/2015 1:30" 28.03197 0
"1/05/2015 2:00" 26.82800 0
"1/05/2015 2:30" 25.22149 0')

solarX[, date := as.POSIXct(MEASDATE, format = "%d/%m/%Y %H:%M")]

#           MEASDATE      rrp exp_kwh                date
# 1: 1/05/2015 0:00 33.99299       0 2015-05-01 00:00:00
# 2: 1/05/2015 0:30 31.53335       0 2015-05-01 00:30:00
# 3: 1/05/2015 1:00 29.37092       0 2015-05-01 01:00:00
# 4: 1/05/2015 1:30 28.03197       0 2015-05-01 01:30:00
# 5: 1/05/2015 2:00 26.82800       0 2015-05-01 02:00:00
# 6: 1/05/2015 2:30 25.22149       0 2015-05-01 02:30:00

str(solarX)
Classes ‘data.table’ and 'data.frame':  6 obs. of  4 variables:
 $ MEASDATE: chr  "1/05/2015 0:00" "1/05/2015 0:30" "1/05/2015 1:00" "1/05/2015 1:30" ...
 $ rrp    : num  34 31.5 29.4 28 26.8 ...
 $ exp_kwh: int  0 0 0 0 0 0
 $ date   : POSIXct, format: "2015-05-01 00:00:00" "2015-05-01 00:30:00" "2015-05-01 01:00:00" "2015-05-01 01:30:00" ...
 - attr(*, ".internal.selfref")=<externalptr>