以下是我的数据示例:
# A tibble: 4 x 3
Squirrel.ID Release.time DetectTime
<int> <S4: Period> <dttm>
1 13097 13H 13M 18S 2017-05-29 18:50:43
2 20948 10H 15M 8S 2017-06-05 08:09:48
3 21853 11H 20M 33S 2017-05-24 21:57:32
4 13088 12H 13M 45S 2017-05-30 08:44:03
我希望将其子集用于hte DetectTime
值出现至少 1.5小时 Release.time
值之前的行。例如,如果我的DetectTime
值为"2017-05-30 06:00:00"
且Release.time
值为"10:00:00"
,那么我希望保留该行。如果我的DetectTime
值为"2017-05-30 10:15:00"
且值Release.time
为"10:00:00"
,那么我会排除该行。
如果有帮助,我正在使用dplyr
和lubridate
。谢谢。
答案 0 :(得分:0)
复制您的数据:
library(lubridate)
df <- data.frame(Squirrel.ID = c(13097, 20948, 21853, 13088),
Release.time = c("13H 13M 18S", "10H 15M 8S", "11H 20M 33S", "12H 13M 45S"),
DetectTime = c("2017-05-29 18:50:43", "2017-06-05 08:09:48", "2017-05-24 21:57:32", "2017-05-30 08:44:03"))
df$Release.time <- hms(df$Release.time)
# Squirrel.ID Release.time DetectTime
#1 13097 13H 13M 18S 2017-05-29 18:50:43
#2 20948 10H 15M 8S 2017-06-05 08:09:48
#3 21853 11H 20M 33S 2017-05-24 21:57:32
#4 13088 12H 13M 45S 2017-05-30 08:44:03
解决方案:
library(lubridate)
df[df$Release.time - hms(strftime(df$DetectTime, format="%H:%M:%S")) >= "1H 30M",]
输出:
# Squirrel.ID Release.time DetectTime
#2 20948 10H 15M 8S 2017-06-05 08:09:48
#4 13088 12H 13M 45S 2017-05-30 08:44:03