Reputation: 177
I am trying to convert a date time to an actual string. But when I use as.character(x) then it returns a value that I can't convert to a date or time.
for instance:
When I extract the data from a particular cell in the dataframe df it returns:
> df[1,6]
# A tibble: 1 x 1
`PLS FFM`
<dttm>
1 2019-12-14 06:47:00
When I try to convert it to a string it returns:
> (as.character(df[1,6]))
[1] "1576306020"
But when I try to convert the string or the datetime () to a date format it returns:
> as.Date(as.character(df[1,6]))
Error in charToDate(x) :
character string is not in a standard unambiguous format
I all ready searched on the internet for hours trying to find a solution, but it's seems that I either doings something wrong or I am honestly really stupid.
Can somebody help me out!?
Thanks,
JOP
Answer thanks to Ronak Shah!
After Ronak Suggested that I should convert the whole vector with format I was able to convert the formatted vector to a Date + time that I can use in my script where I make a lot of calculations with the time. I Used:
df$'PLS FFM' <- format(df$'PLS FFM')
df$'PLS FFM' <- as.POSIXCT(df$'PLS FFM')
Thanks to Ronak
Upvotes: 1
Views: 4522
Reputation: 389235
Since df[1,6]
is still a tibble using as.character
doesn't work on it. You need to pass a vector to as.character
which can be done with
as.character(df$`PLS FFM`)
#OR
#as.character(df[["PLS FFM"]])
We can also use format
format(df$`PLS FFM`)
If you need a specific value from the dataframe, use
as.character(df$`PLS FFM`[1])
Upvotes: 3