r - 将带 MM:SS.MS 的时间戳转换为不带日期 R 的 MM:SS.MS
问题描述
我需要用分钟、秒和毫秒转换视频时间戳,以便在逻辑中用于过滤。(即:VideoTime < 00:02:30)我只需要对我的部分数据进行子集化以进行统计。我在 Mac OSX HighSierra 上使用 R Studio 1.2.1335。
我尝试将一个因子转换为一个字符到 Posix.ct、strptime、strftime、hms (lubridate)、as.duration(lubridate) 和 chron。
我希望下面的格式正确,因为我尝试使用 Markdown 指令(但通常在 Markdown 中编写代码我使用 ctrl+option+i,这在此处不起作用)。
options(digits.secs=2)
test1=strftime(test1, format='%M:%OS2')
test2=hms(EAllFilt$Video.Time)
test3=test2 %>% as.duration(dminutes(2), dseconds(4))
test4=strptime(EAllFilt$Video.Time, '%M:%SO2')
test5=as.chron(times(EAllFilt$Video.Time, format="m:s"))```
>EAllFilt$Video.Time=as.character(EAllFilt$Video.Time)
> str(EAllFilt)
'data.frame': 7665 obs. of 4 variables:
$ Video.Time: chr "00:00.0" "00:00.2" "00:00.4" "00:00.6" ...
$ Valence : num 379 378 358 218 612 686 698 688 684 689 ...
$ Subject : Factor w/ 5 levels "50132","50142",..: 5 5 5 5 5 5 5 5 5 5 ...
$ Session : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
> head(EAllFilt$Video.Time)
[1] "00:00.0" "00:00.2" "00:00.4" "00:00.6" "00:00.8" "00:01.0"
> tail(EAllFilt$Video.Time)
[1] "02:24.4" "02:24.6" "02:24.8" "02:25.0" "02:25.2" "02:25.4"
> options(digits.secs=2)
> test1=strftime(test1, format='%M:%OS2')
Error in as.POSIXlt.character(x, tz = tz) :
character string is not in a standard unambiguous format
> class(test1)
[1] "character"
> tail(test1)
[1] "02:24.4" "02:24.6" "02:24.8" "02:25.0" "02:25.2" "02:25.4"
> test2=hms(EAllFilt$Video.Time)
> class(test2)
[1] "Period"
attr(,"package")
[1] "lubridate"
> str(test2)
Formal class 'Period' [package "lubridate"] with 6 slots
..@ .Data : num [1:7665] 0 2 4 6 8 0 2 4 6 8 ...
..@ year : num [1:7665] 0 0 0 0 0 0 0 0 0 0 ...
..@ month : num [1:7665] 0 0 0 0 0 0 0 0 0 0 ...
..@ day : num [1:7665] 0 0 0 0 0 0 0 0 0 0 ...
..@ hour : num [1:7665] 0 0 0 0 0 0 0 0 0 0 ...
..@ minute: num [1:7665] 0 0 0 0 0 1 1 1 1 1 ...
> head(test2)
[1] "0S" "2S" "4S" "6S" "8S" "1M 0S"
> tail(test2)
[1] "2H 24M 4S" "2H 24M 6S" "2H 24M 8S" "2H 25M 0S" "2H 25M 2S" "2H 25M 4S"
> test3=test2 %>% as.duration(dminutes(2), dseconds(4))
Error in .local(x, ...) : unused arguments (dminutes(2), dseconds(4))
> class(test3)
[1] "Duration"
attr(,"package")
[1] "lubridate"
> str(test3)
Formal class 'Duration' [package "lubridate"] with 1 slot
..@ .Data: num [1:7665] NA NA NA NA NA NA NA NA NA NA ...
> tail(test3)
[1] NA NA NA NA NA NA
> test4=strptime(EAllFilt$Video.Time, '%M:%SO2')
> class(test4)
[1] "POSIXlt" "POSIXt"
> str(test4)
POSIXlt[1:7665], format: NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA ...
> head(test4)
[1] NA NA NA NA NA NA
> tail(test4)
[1] NA NA NA NA NA NA
> test5=as.chron(times(EAllFilt$Video.Time, format="m:s"))
Error in convert.times(times., fmt) : format m:s may be incorrect
Test 2, using lubridate, seems to be closest to what I'l need but it thinks that I'm using HH:MM.SS and rather than MM:SS.ms (see "tail(test4)"
解决方案
我找到的最佳答案:
test6=period_to_seconds(hms(EAllFilt$Video.Time))
class(test6)
[1] "numeric"
head(test6)
[1] 0 2 4 6 8 60
tail(test6) #this one is best!
[1] 8644 8646 8648 8700 8702 8704
它将毫秒数据保存为秒,但仍可用于我的目的
推荐阅读
- android - 从多个联系号码列表中选择一个联系号码
- css - 如何为角度组件应用全局动画背景?
- python - 如何在美汤中返回没有属性的标签?
- ruby - 如何使用页面对象和 watir 生成魅力报告
- swift - 将 NSColor.textColor 快速转换为 cgColor 会忽略主题
- d3.js - 如何获取特定国家/地区的格式规则?
- javascript - 反应:输入不更新不同的值
- node.js - 同时搜索集合字段和填充字段
- amazon-web-services - 为什么实例停止时不释放私有IP?
- c++ - 使用 ShellExecuteEX 打开 powershell 会导致 PS 会话不导入所有可用模块