Kenneth Singh
Kenneth Singh

Reputation: 405

Converting to Date Time Using as.POSIXct

I have a column "DateTime". Example value: 2016-12-05-16.25.54.875000

When I import this, R reads it as a Factor.

Now, when I sort the dataset by decreasing "DateTime", the maximum DateTime is 23 June 2017. When I use DateTime = as.POSIXct(DateTime), it changes to 22 June 2017. How is this happening?

P.S. I am running this R script in Power BI.

Upvotes: 0

Views: 8610

Answers (1)

Umberto
Umberto

Reputation: 1421

So some comments first. When you read strings in R, unless you specify otherwise they are imported as factors. You can use the Option

Trying what @Disco Superfly has suggested works if you define the data as a string in R

> a <- "2016-12-05-16.25.54.875000"
> as.POSIXct(a, format="%Y-%m-%d-%H.%M.%S") 
[1] "2016-12-05 16:25:54 CET"
> as.POSIXct(a)
[1] "2016-12-05 CET"

Is not clear what you are saying about the fact that the data is being changed. Can you give a reproducible example?

To summarize, if your Dates are strings that what other have already suggested works perfectly. I suppose you are trying to do more than what you are explaining and therefore I don't understand what you are saying exactly.

Upvotes: 0

Related Questions