0

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.

Kenneth Singh
  • 335
  • 1
  • 3
  • 15

1 Answers1

0

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.

Umberto
  • 1,387
  • 1
  • 13
  • 29
  • Hi, thanks for the response. Si when I do as.POSIXct in R, it converts without any problem. This issue arises when I do the same using the R scripting option in Power BI. – Kenneth Singh Jul 05 '17 at 04:45
  • Check this link https://thusithamabotuwana.wordpress.com/2016/08/27/working-with-date-fields-in-r-and-power-bi/#ampshare=https://thusithamabotuwana.wordpress.com/2016/08/27/working-with-date-fields-in-r-and-power-bi/ it maybe helpful. The same happens with SAS, since it uses a different origin to calculate Dates. – Umberto Jul 05 '17 at 07:04