首页 > 解决方案 > R:以秒为单位的变量“difftime”转换为以天为单位的变量“difftime”

问题描述

我有以下x类向量,difftime以秒为单位表示一些时间跨度:

> x
Time differences in secs
[1]       0       0 7948800       0

> class(x)
[1] "difftime"

我想转换变量以便以天而不是秒来表示每个值

我怎么能得到它?

标签: rtime

解决方案


要么units=按照 RonakShah 的建议使用,

x <- difftime(Sys.time() + c(0, 100, 3*86400), Sys.time(), units = "days")
x
# Time differences in days
# [1] 0.000000000 0.001157407 3.000000000

或事后更改单位。

x <- difftime(Sys.time() + c(0, 100, 3*86400), Sys.time())
x
# Time differences in secs
# [1]      0    100 259200
units(x) <- "days"
x
# Time differences in days
# [1] 0.000000000 0.001157407 3.000000000

推荐阅读