将带有 MM:SS.MS 的时间戳转换为 MM:SS.MS 没有日期 R

Convert Timestamp with MM:SS.MS to MM:SS.MS without date R

我需要将视频时间戳转换为分、秒和毫秒,以便在逻辑过滤中使用。 (即: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

它将毫秒数据保存为秒,但它仍然可以用于我的目的