R:如何处理没有日期的时间? [英] R: How to handle times without dates?

查看:189
本文介绍了R:如何处理没有日期的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据包括 Date 以及时间输入时间退出。后两者包含这样的数据: 08:02 12:02 23: 45 等。

I have data which includes Date as well as Time enter and Time exit. These latter two contain data like this: 08:02, 12:02, 23:45 etc.

我想操纵 Time eXXX 数据 - 例如,减去时间退出输入以计算持续时间,或绘制时间的分布输入时间退出,例如查看大多数条目是否在10:00之前,或者大多数退出是在17:00之后。

I would like to manipulate the Time eXXX data - for example, substract Time enter from Time exit to work out duration, or plot the distributions of Time enter and Time exit, e.g. to see if most entries are before 10:00, or if most exits are after 17:00.

我查看的所有包都需要在时间,例如 01/02/2012 12:33

All the packages I've looked at require a date to precede the time, e.g. 01/02/2012 12:33.

这是可能的,还是应该简单地将相同的日期添加到每次为了计算?这个看起来有点混乱!

Is this possible, or should I simply append an identical date to every time for the sake of calculations? This seem a bit messy!

推荐答案

使用times发现在chron包中:

Use the "times" class found in the chron package:

library(chron)

Enter <- c("09:12", "17:01")
Enter <- times(paste0(Enter, ":00"))

Exit <-  c("10:15", "18:11")
Exit <- times(paste0(Exit, ":00"))

Exit - Enter # durations

sum(Enter < "10:00:00") # no entering before 10am
mean(Enter < "10:00:00") # fraction entering before 10am

sum(Exit >  "17:00:00") # no exiting after 5pm
mean(Exit >  "17:00:00") # fraction exiting after 5pm

table(cut(hours(Enter), breaks = c(0, 10, 17, 24))) # Counts for indicated hours   
 ## (0,10] (10,17] (17,24] 
 ##      1       1       0 

table(hours(Enter))  # Counts of entries each hour
## 9 17 
## 1  1

stem(hours(Enter), scale = 2)
## The decimal point is at the |

##   9 | 0
##  10 | 
##  11 | 
##  12 | 
##  13 | 
##  14 | 
##  15 | 
##  16 | 
##  17 | 0

图形:

tab <- c(table(Enter), -table(Exit))  # Freq at each time.  Enter is pos; Exit is neg.
plot(times(names(tab)), tab, type = "h", xlab = "Time", ylab = "Freq")
abline(v = c(10, 17)/24, col = "red", lty = 2) # vertical red lines
abline(h = 0)  # X axis

这篇关于R:如何处理没有日期的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆