R中的格式时间与日期无关

时间:2017-03-20 00:42:36

标签: r posixct strptime posixlt chron

如何设置独立于R中日期的时间?我有一个时间向量,我想添加和减去;但是,当我将矢量格式化为POSIXct(),POSIXlt()或strptime()时,R会附加一个日期:

> sample
     V1
1  1:23
2  5:20
3  2:28
4  1:38
5  6:27
6  2:19
7  0:22
8  6:25
9  5:07
10 2:28
11 2:33
12 0:03
13 3:58
>       str(sample)
'data.frame':   13 obs. of  1 variable:
 $ V1: Factor w/ 12 levels "0:03","0:22",..: 3 10 6 4 12 5 2 11 9 6 ...
>       sample.1 = strptime(sample$V1, format = "%M:%S")
>       sample.1
 [1] "2017-03-18 00:01:23 PDT" "2017-03-18 00:05:20 PDT" "2017-03-18 00:02:28 PDT"
 [4] "2017-03-18 00:01:38 PDT" "2017-03-18 00:06:27 PDT" "2017-03-18 00:02:19 PDT"
 [7] "2017-03-18 00:00:22 PDT" "2017-03-18 00:06:25 PDT" "2017-03-18 00:05:07 PDT"
[10] "2017-03-18 00:02:28 PDT" "2017-03-18 00:02:33 PDT" "2017-03-18 00:00:03 PDT"
[13] "2017-03-18 00:03:58 PDT"
>       str(sample.1)
 POSIXlt[1:13], format: "2017-03-18 00:01:23" "2017-03-18 00:05:20" "2017-03-18 00:02:28" ...
>       sample.2  = as.POSIXct(sample$V1, format = "%M:%S")
>       sample.2
 [1] "2017-03-18 00:01:23 PDT" "2017-03-18 00:05:20 PDT" "2017-03-18 00:02:28 PDT"
 [4] "2017-03-18 00:01:38 PDT" "2017-03-18 00:06:27 PDT" "2017-03-18 00:02:19 PDT"
 [7] "2017-03-18 00:00:22 PDT" "2017-03-18 00:06:25 PDT" "2017-03-18 00:05:07 PDT"
[10] "2017-03-18 00:02:28 PDT" "2017-03-18 00:02:33 PDT" "2017-03-18 00:00:03 PDT"
[13] "2017-03-18 00:03:58 PDT"
>       str(sample.2)
 POSIXct[1:13], format: "2017-03-18 00:01:23" "2017-03-18 00:05:20" "2017-03-18 00:02:28" ...
>       sample.3  = as.POSIXlt(sample$V1, format = "%M:%S")
>       sample.3
 [1] "2017-03-18 00:01:23 PDT" "2017-03-18 00:05:20 PDT" "2017-03-18 00:02:28 PDT"
 [4] "2017-03-18 00:01:38 PDT" "2017-03-18 00:06:27 PDT" "2017-03-18 00:02:19 PDT"
 [7] "2017-03-18 00:00:22 PDT" "2017-03-18 00:06:25 PDT" "2017-03-18 00:05:07 PDT"
[10] "2017-03-18 00:02:28 PDT" "2017-03-18 00:02:33 PDT" "2017-03-18 00:00:03 PDT"
[13] "2017-03-18 00:03:58 PDT"
>       str(sample.3)
 POSIXlt[1:13], format: "2017-03-18 00:01:23" "2017-03-18 00:05:20" "2017-03-18 00:02:28" ...

之前我问过这个,它被标记为重复。但是,它被标记为重复的问题没有得到回答。我试图在编辑中解释这一点,但忽略了编辑。事实上,我发现这个问题在SO和其他网站上被多次提出;但是,我从没见过令人满意的答案。我发现的最接近的事情是:

> library(chron)
>       sample.4 = ms(sample$V1)
>       sample.4
 [1] "1M 23S" "5M 20S" "2M 28S" "1M 38S" "6M 27S" "2M 19S" "22S"    "6M 25S" "5M 7S"  "2M 28S"
[11] "2M 33S" "3S"     "3M 58S"
>       str(sample.4)
Formal class 'Period' [package "lubridate"] with 6 slots
  ..@ .Data : num [1:13] 23 20 28 38 27 19 22 25 7 28 ...
  ..@ year  : num [1:13] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ month : num [1:13] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ day   : num [1:13] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ hour  : num [1:13] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ minute: num [1:13] 1 5 2 1 6 2 0 6 5 2 ...

chron :: ms()允许我将样本$ V1格式化为"句号",而不插入不需要的日期;但是,当我尝试添加或减去时,我遇到了问题:

> a = sample.4[1] + sample.4[2] + sample.4[13]
>       b = sample.4[2] - sample.4[3]
>       a
[1] "9M 101S"
>       b
[1] "3M -8S"

显然" a"应该等于" 10:41"而不是" 9M 101S"和" b"应该等于" 2:52"而不是" 3M -8S"。操作是准确的,但它的显示失败了目的。有没有办法简单地以有意义的方式显示这些结果?或者,是否还有一些其他功能可以在没有日期的情况下格式化时间?谢谢你的帮助。

0 个答案:

没有答案