gpt4 book ai didi

r - R:如何处理没有日期的时间?

转载 作者:行者123 更新时间:2023-12-03 20:28:45 25 4
gpt4 key购买 nike

我有包括Date以及Time enterTime exit的数据。后两个包含如下数据:08:0212:0223:45等。

我想操纵Time eXXX数据-例如,从Time enter减去Time exit算出持续时间,或绘制Time enterTime exit的分布,例如查看大多数条目是在10:00之前,还是大多数退出是在17:00之后。

我查看过的所有软件包都需要在日期之前加上日期,例如01/02/2012 12:33

这是否可能,或者我应该为每次计算简单地在每个时间后面附加一个相同的日期?这看起来有点混乱!

最佳答案

使用在chron package中找到的"times"类:

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


screenshot

关于r - R:如何处理没有日期的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22659947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com