英文:
Convert Seconds to Hours in R
问题
我有一个以秒为单位的列,我想将其转换为小时。最好是将小时以小数格式表示,例如8.5小时,而不是将小时和分钟分开列出。任何帮助将不胜感激。
英文:
I have a column that is in seconds and I want to convert it into hours. I preferably want the hours into a decimal format such as 8.5 hours instead of hours and minutes in separate columns. Any help would be much appreciated.
答案1
得分: 1
以下是翻译好的部分:
这里有两种方法。如果x是一个向量,这两种方法也都适用。第一种方法利用一小时有3600秒这一事实。第二种方法较长,但不使用特殊数字。不需要使用任何包。
x <- 30600 # 测试数据
x / 3600
## [1] 8.5
as.numeric(as.difftime(x, units = "secs"), units = "hours")
## [1] 8.5
英文:
Here are two methods. Both also work if x is a vector. The first uses the fact that there are 3600 seconds in an hour. The second is longer but does not use special numbers. No packages are used.
x <- 30600 # test data
x / 3600
## [1] 8.5
as.numeric(as.difftime(x, units = "secs"), units = "hours")
## [1] 8.5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论