从 R 中的时间向量中减去 10 分钟? [英] Subtracting 10 minutes each from a vector of times in R?

查看:67
本文介绍了从 R 中的时间向量中减去 10 分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列这样的时间:

"2:43::00 PM"
"2:43:01 PM"
"2:43:05 PM"
"2:43:06 PM"
"2:43:07 PM"

我想转到每一行并从每个时间中减去 10 分钟,使新列看起来像这样:

I want to go to each of the row and subtract 10 minutes from each of the time such that the new column looks like this:

"2:33::00 PM"
"2:33:01 PM"
"2:33:05 PM"
"2:33:06 PM"
"2:33:07 PM"

我正在使用

difftime(strptime(data$time[i],format="%H:%M:%S"),strptime("00:10:00",format="%H:%M:%S"))

,但它没有显示正确的结果.

, but it is not showing correct results.

我该如何解决这个问题?

How can i approach this problem?

推荐答案

尝试

format(strptime(str1, format = "%I:%M:%S %p") - 10*60, "%I:%M:%S %p")
#[1] "02:33:00 PM" "02:33:01 PM" "02:33:05 PM" "02:33:06 PM" "02:33:07 PM"

数据

str1 <- c("2:43:00 PM", "2:43:01 PM", "2:43:05 PM", "2:43:06 PM", "2:43:07 PM")

这篇关于从 R 中的时间向量中减去 10 分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆