转换周期(百分之一秒)r

时间:2016-04-29 18:02:31

标签: r

我正在尝试转换以下形式的矢量:

data$Time[1:10]

[1] 0:00.00 0:00.01 0:00.02 0:00.03 0:00.04 0:00.05 0:00.06 0:00.07 0:00.08 0:00.09
573394 Levels: 0:00.00 0:00.01 0:00.02 0:00.03 0:00.04 0:00.05 0:00.06 0:00.07 0:00.08 0:00.09 0:00.10 0:00.11 0:00.12 0:00.13 0:00.14 ... 9:59.99

请注意,这是一个因素形式

class(data$Time)

factor

我已尝试过以下

hms(data$Time[1:10])

[1] "0S" "1S" "2S" "3S" "4S" "5S" "6S" "7S" "8S" "9S"

它看到1/100秒作为秒!

同样的事情
period_to_seconds(hms(data$Time[1:10]))

[1] 0 1 2 3 4 5 6 7 8 9

我需要能够提取时间(具有要求的准确性)以便能够减去和计算周期。请注意,这些文件将延长到几个小时。因此,对于HH:MM:SS.00有益的解决方案将不胜感激

另一种方法仅在您拥有H M S或M S数据时才有效:

Test <- c('03:5.05', '1:03.05.05')
tmp <- strptime(as.character(Test),"%H:%M:%OS")
tmp
[1] NA NA
tmp <- strptime(as.character(Test),"%M:%OS")
tmp
[1] "2016-04-30 00:03:05.05 CDT" "2016-04-30 00:01:03.05 CDT

(必须删除小时数)

3 个答案:

答案 0 :(得分:1)

if (!isset($_GET['id'])) {
            ^^^^^^^^--check if the parameter exists
}
$trkid=$_GET['id'];
       ^---try to use the parameter ANYWAYS, even if it doesn't exist.

答案 1 :(得分:0)

在lubridate包中有一个ms函数只读取分钟和秒。

Test <- c('0:00.02', '9:59.99')

library(lubridate)
Test %>% ms() %>% period_to_seconds()

[1]   0.02 599.99

答案 2 :(得分:0)

根据Jorg的回答。我想我能够解决我的问题。我正在使用的文件延长了几个小时(每个点代表0.01秒)。所以我拆分了向量(数据$ Time)并为前三个点应用了MS脚本,并为以下内容应用了HMS脚本:

options(digits.secs = 2)

tmp1 <- strptime(as.character(data$Time[1:360000]),"%M:%OS")
tmp2 <- strptime(as.character(data$Time[-(1:360000)]),"%H:%M:%OS")

tmp1_numeric <-as.numeric(strftime(tmp1,'%OS'))+60*as.numeric(strftime(tmp1,'%M'))+60*60*as.numeric(strftime(tmp1,'%H'))
tmp2_numeric <-as.numeric(strftime(tmp2,'%OS'))+60*as.numeric(strftime(tmp2,'%M'))+60*60*as.numeric(strftime(tmp2,'%H'))

tmp_numeric <- c(tmp1_numeric, tmp2_numeric)