将秒转换为小时在 R 中

huangapple go评论119阅读模式
英文:

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 &lt;- 30600 # test data

x / 3600
## [1] 8.5

as.numeric(as.difftime(x, units = &quot;secs&quot;), units = &quot;hours&quot;)
## [1] 8.5

huangapple
  • 本文由 发表于 2023年2月23日 22:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定