Reputation: 349
> resulttable[3,1]
UTC FORECAST TIME
1: 2018-05-01 00:30:00
> as.POSIXct(resulttable[3,1],format="%Y-%m-%d %H:%M:%S",tz="UTC")
Error in as.POSIXct.default(resulttable[3, 1], format = "%Y-%m-%d %H:%M:%S", :
do not know how to convert 'resulttable[3, 1]' to class “POSIXct”
I don't understand the error, because the first column of resulttable is formatted as posixct
> lapply(resulttable,class)
$`UTC FORECAST TIME`
[1] "POSIXct" "POSIXt"
$`UTC FORECAST RECEIVE TIME`
[1] "POSIXct" "POSIXt"
Why does as.POSIXct(resulttable[3,1],format="%Y-%m-%d %H:%M:%S",tz="UTC")
lead to an error?
edit: Here the output of str:
> str(resulttable[3,1])
Classes ‘data.table’ and 'data.frame': 1 obs. of 1 variable:
$ UTC FORECAST TIME: POSIXct, format: "2018-05-01 00:30:00"
- attr(*, ".internal.selfref")=<externalptr>
Upvotes: 1
Views: 1782
Reputation: 76402
Note the output of str(resulttable[3,1])
. It says that it is an object of classes data.table
and data.frame
. Therefore, you must extract the column or columns you want to convert to POSIXct
with the standard extraction operators.
resulttable[3, 1][[1]] # a vector
resulttable[3, 1]$`UTC FORECAST TIME` # the same vector
Note that the column name, UTC FORECAST TIME
has spaces in it so you need to put it between back quotes.
Then, in order to do the conversion you can use any of the above forms.
as.POSIXct(resulttable[3,1][[1]], format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
as.POSIXct(resulttable[3,1]$`UTC FORECAST TIME`, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
Upvotes: 2