Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将整数转换为时间并在R中进行计算_R_Date_Time_Integer_Datetime Conversion - Fatal编程技术网

将整数转换为时间并在R中进行计算

将整数转换为时间并在R中进行计算,r,date,time,integer,datetime-conversion,R,Date,Time,Integer,Datetime Conversion,我在约会时间上遇到了问题。我的数据框是这样的,我想找出每个人看电视的持续时间 Start.Time <- c(193221,201231,152324,182243,123432,192245) End.Time <- c(202013,211232,154521,183422,133121,201513) cbind(Start.Time,End.Time) 我的结果完全错了 [1] "2015-11-03 05:40:21 GMT" "2015-11-03 07:53:51 G

我在约会时间上遇到了问题。我的数据框是这样的,我想找出每个人看电视的持续时间

Start.Time <- c(193221,201231,152324,182243,123432,192245)
End.Time <- c(202013,211232,154521,183422,133121,201513)
cbind(Start.Time,End.Time)
我的结果完全错了

[1] "2015-11-03 05:40:21 GMT" "2015-11-03 07:53:51 GMT"
[3] "2015-11-02 18:18:44 GMT" "2015-11-03 02:37:23 GMT"
[5] "2015-11-02 10:17:12 GMT" "2015-11-03 05:24:05 GMT"
例如,我希望193221变成19:32:21 HH:MM:SS


有没有一个软件包可以轻松地进行转换?如果可能的话,我不想显示日期,只想显示时间。

您可以将数字转换为实际时间戳(POSIXct格式),如下所示:

Start.Time <- c(193221,201231,152324,182243,123432,192245)
Start.POSIX <- as.POSIXct(as.character(Start.Time), format = "%H%M%S")
Start.POSIX
## [1] "2015-12-19 19:32:21 CET" "2015-12-19 20:12:31 CET" "2015-12-19 15:23:24 CET"
## [4] "2015-12-19 18:22:43 CET" "2015-12-19 12:34:32 CET" "2015-12-19 19:22:45 CET"
当您打印POSIXct对象时(正如我在上面使用
Start.POSIX
时所做的那样),它们被准确地转换为字符,并被打印出来。您可以看到这一点,因为日期周围有
。您可以控制打印时使用的格式,因此只能按如下方式打印时间:

format(Start.POSIX, "%H:%M:%S")
## [1] "19:32:21" "20:12:31" "15:23:24" "18:22:43" "12:34:32" "19:22:45"

lubridate软件包可能会对您有所帮助
End.Time <- c(202013,211232,154521,183422,133121,201513)
End.POSIX <- as.POSIXct(as.character(End.Time), format = "%H%M%S")
End.POSIX - Start.POSIX
## Time differences in mins
## [1] 47.86667 60.01667 21.95000 11.65000 56.81667 52.46667
format(Start.POSIX, "%H:%M:%S")
## [1] "19:32:21" "20:12:31" "15:23:24" "18:22:43" "12:34:32" "19:22:45"