R格式回答时差

发布于 2024-12-22 18:43:06 字数 1247 浏览 1 评论 0原文

我正在尝试格式化减去 2 次的答案。

下面是一个示例:

> timer2$tdif2 <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S:%OS") - strptime(as.character(timer2$time2), "%H:%M:%S:%OS"))
> timer2$tdif1 <- as.numeric(strptime(as.character(timer2$time2), "%H:%M:%S%OS") - strptime(as.character(timer2$time1), "%H:%M:%S%OS"))
> timer2$tdif2 <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S:%OS") - strptime(as.character(timer2$time2), "%H:%M:%S:%OS"))
> timer2$tdifMax <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S.%OS") - strptime(as.character(timer2$time1), "%H:%M:%S.%OS"))
> head(timer2)
         time1        time2        time3         tdif1 tdif2 tdifMax
1 08:00:20.799 08:00:20.799 08:00:20.799  0.0000000000    NA       0
2 08:00:21.996 08:00:22.071 08:00:23.821 -0.9249999523    NA       2
3 08:00:29.200 08:00:29.200 08:00:29.591  0.0000000000    NA       0
4 08:00:31.073 08:00:31.372 08:00:31.384  0.2990000248    NA       0
5 08:00:31.867 08:00:31.867 08:00:31.971  0.0000000000    NA       0
6 08:00:37.174 08:00:38.073 08:00:38.153 -0.1010000706    NA       1

我在 tdif1、tdif2 和 tdif3 中对秒的部分使用了不同的格式化公式,但它们都没有给出以秒和秒的部分为单位的答案(对于 tdif[2],它应该是 0.075)。有什么建议吗?

I'm trying to format the answer from subtracting 2 times.

Here's an example:

> timer2$tdif2 <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S:%OS") - strptime(as.character(timer2$time2), "%H:%M:%S:%OS"))
> timer2$tdif1 <- as.numeric(strptime(as.character(timer2$time2), "%H:%M:%S%OS") - strptime(as.character(timer2$time1), "%H:%M:%S%OS"))
> timer2$tdif2 <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S:%OS") - strptime(as.character(timer2$time2), "%H:%M:%S:%OS"))
> timer2$tdifMax <- as.numeric(strptime(as.character(timer2$time3), "%H:%M:%S.%OS") - strptime(as.character(timer2$time1), "%H:%M:%S.%OS"))
> head(timer2)
         time1        time2        time3         tdif1 tdif2 tdifMax
1 08:00:20.799 08:00:20.799 08:00:20.799  0.0000000000    NA       0
2 08:00:21.996 08:00:22.071 08:00:23.821 -0.9249999523    NA       2
3 08:00:29.200 08:00:29.200 08:00:29.591  0.0000000000    NA       0
4 08:00:31.073 08:00:31.372 08:00:31.384  0.2990000248    NA       0
5 08:00:31.867 08:00:31.867 08:00:31.971  0.0000000000    NA       0
6 08:00:37.174 08:00:38.073 08:00:38.153 -0.1010000706    NA       1

I've used a different formatting formula for parts of a second in tdif1, tdif2 and tdif3 but none of them give the answer in seconds and parts of a second (for tdif[2] it should be .075). Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-12-29 18:43:06

您的格式错误,您的数据采用 "%H:%M:%OS" 格式。另外我建议你用数字进行数学计算 - 这总是给你几秒钟。

所以你的例子:

sec <- function(x) as.numeric(strptime(x, "%H:%M:%OS"))

timer2$tdif1   <- sec(timer2$time2) - sec(timer2$time1)
timer2$tdif2   <- sec(timer2$time3) - sec(timer2$time2)
timer2$tdifMax <- sec(timer2$time3) - sec(timer2$time1)

和输出:

> head(timer2)
         time1        time2        time3      tdif1      tdif2   tdifMax
1 08:00:20.799 08:00:20.799 08:00:20.799 0.00000000 0.00000000 0.0000000
2 08:00:21.996 08:00:22.071 08:00:23.821 0.07500005 1.75000000 1.8250000
3 08:00:29.200 08:00:29.200 08:00:29.591 0.00000000 0.39100003 0.3910000
4 08:00:31.073 08:00:31.372 08:00:31.384 0.29900002 0.01200008 0.3110001
5 08:00:31.867 08:00:31.867 08:00:31.971 0.00000000 0.10399985 0.1039999
6 08:00:37.174 08:00:38.073 08:00:38.153 0.89899993 0.08000016 0.9790001

You got the format wrong, your data is in "%H:%M:%OS" format. Also I'd suggest you do the math in numerics - that gives you always seconds.

So your example:

sec <- function(x) as.numeric(strptime(x, "%H:%M:%OS"))

timer2$tdif1   <- sec(timer2$time2) - sec(timer2$time1)
timer2$tdif2   <- sec(timer2$time3) - sec(timer2$time2)
timer2$tdifMax <- sec(timer2$time3) - sec(timer2$time1)

and the output:

> head(timer2)
         time1        time2        time3      tdif1      tdif2   tdifMax
1 08:00:20.799 08:00:20.799 08:00:20.799 0.00000000 0.00000000 0.0000000
2 08:00:21.996 08:00:22.071 08:00:23.821 0.07500005 1.75000000 1.8250000
3 08:00:29.200 08:00:29.200 08:00:29.591 0.00000000 0.39100003 0.3910000
4 08:00:31.073 08:00:31.372 08:00:31.384 0.29900002 0.01200008 0.3110001
5 08:00:31.867 08:00:31.867 08:00:31.971 0.00000000 0.10399985 0.1039999
6 08:00:37.174 08:00:38.073 08:00:38.153 0.89899993 0.08000016 0.9790001
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文