Home » excel » r – Excel time not always right converted

r – Excel time not always right converted

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m importing Date-time values from and when converting in in , some results are good other are wrong. Here are my raw data (from excel):

         time excel
1   43033.35662037037
2   43033.35662037037
3   43033.35662037037
4   43033.35662037037
5   43033.35663194444
6   43033.35663194444
7   43033.35663194444
8   43033.35663194444
9   43033.35664351852
10  43033.35664351852
11  43033.35664351852
12  43033.35664351852
13  43033.35665509259
14  43033.35665509259
15  43033.35665509259
16  43033.35665509259
17  43033.35666666667
18  43033.35666666667
19  43033.35666666667
20  43033.35666666667
21  43033.35667824074

Here is my code:

raw_time$`time excel` = as.numeric(raw_time$`time excel`)
r_time <- data.frame(as.Date(raw_time$`time excel`, origin = "1899-12-30"))
colnames(r_time)<-"time R"
r_time$`time R` <- as.POSIXct(r_time$`time R` , format =  "%d.%m.%Y %H:%M:%OS6")

And here is what I get:

        time excel                   time R

1   43033.35662037037   2017-10-25 10:33:32
2   43033.35662037037   2017-10-25 10:33:32
3   43033.35662037037   2017-10-25 10:33:32
4   43033.35662037037   2017-10-25 10:33:32
5   43033.35663194444   2017-10-25 10:33:33
6   43033.35663194444   2017-10-25 10:33:33
7   43033.35663194444   2017-10-25 10:33:33
8   43033.35663194444   2017-10-25 10:33:33
9   43033.35664351852   2017-10-25 10:33:34
10  43033.35664351852   2017-10-25 10:33:34
11  43033.35664351852   2017-10-25 10:33:34
12  43033.35664351852   2017-10-25 10:33:34
13  43033.35665509259   2017-10-25 10:33:34
14  43033.35665509259   2017-10-25 10:33:34
15  43033.35665509259   2017-10-25 10:33:34
16  43033.35665509259   2017-10-25 10:33:34
17  43033.35666666667   2017-10-25 10:33:36
18  43033.35666666667   2017-10-25 10:33:36
19  43033.35666666667   2017-10-25 10:33:36
20  43033.35666666667   2017-10-25 10:33:36
21  43033.35667824074   2017-10-25 10:33:37

As you see, I get four times 32″ (what ist right), 4 times 33″ what is also right, but then I get 8 times 34″ what is wrong (check the raw values of excel).

What am I doing wrong? Appreciate your help…

How to&Answers:

I think when you convert to dates, you lose precision. Change from excel date to date-time directly by converting days into seconds and use as.POSIXct.

df$Time_R <- as.POSIXct(df$time_excel * 60 * 60 * 24, origin="1899-12-30")