how to convert excel internal coding for hours to hours in R? [duplicate]

与世无争的帅哥 提交于 2019-12-12 14:26:01

问题


I have a hour variable stored as excel internal coding in R. See (https://www.ablebits.com/office-addins-blog/2015/06/23/excel-time-format-now-time-functions/), for example 0.4305556 is equivalent to 10:20:00 AM. How to convert 0.4305556 to 10:20:00 AM in R. After this i need to substract times to find time diferennces.


回答1:


You can use hms:

library(hms)

hms(days = 0.4305556)
#> 10:20:00.00384

Subtract two hms objects to return a difftime object with the time difference in seconds:

hms(days = 0.5) - hms(days = 0.25)
#> Time difference of 21600 secs

Or to get the time difference in a different unit use the difftime function:

difftime(hms(days = 0.5), hms(days = 0.25), units = "hours")
#> Time difference of 6 hours


来源:https://stackoverflow.com/questions/57482012/how-to-convert-excel-internal-coding-for-hours-to-hours-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!