我有什么:
a <- c(49, 55, '2017-11-01', '13:54:57', '2017-11-01', '14:13:46')
b <- c(173, 171, '2017-11-01', '14:16:19', '2017-11-01', '14:23:34')
c <- c(171, 173, '2017-11-01', '14:57:01', '2017-11-01', '15:05:11')
d <- c(55, 49, '2017-11-01', '15:07:07', '2017-11-01', '15:27:30')
e <- as.data.frame(t(data.frame(a, b, c, d)))
colnames(e) <- c('start', 'stop', 'fdate', 'ftime', 'tdate', 'ttime')
e
start stop fdate ftime tdate ttime
49 55 2017-11-01 13:54:57 2017-11-01 14:13:46
173 171 2017-11-01 14:16:19 2017-11-01 14:23:34
171 173 2017-11-01 14:57:01 2017-11-01 15:05:11
55 49 2017-11-01 15:07:07 2017-11-01 15:27:30
我想在下一个条件中合并行:
如果前一行ftime
和ttime
之间的差异小于30分钟,我们必须写一行:
start
,
最后一行stop
,
第一行ftime
,
最后一行ttime
我想要的例子:
start stop fdate ftime tdate ttime
49 171 2017-11-01 13:54:57 2017-11-01 14:23:34
171 49 2017-11-01 14:57:01 2017-11-01 15:27:30
答案 0 :(得分:1)
尝试以下
library(lubridate)
library(dplyr)
myfun <- function(e) {
dt_f <- ymd_hms(paste(e$fdate, e$ftime, sep=" ")) # convert to Date
lag_dt_t <- ymd_hms(paste(dplyr::lag(e$tdate, 1, default=head(e$fdate, 1)), dplyr::lag(e$ttime, 1, default=NA), sep=" ")) # convert to Date
lgl <- dt_f - lag_dt_t <= make_difftime(minute = 30) # is difference <= 30 min?
lgl[1] <- FALSE # Always set first row to FALSE since there is no previous row to compare to
return(cumsum(!lgl)) # Returns `grp` values
}
temp <- e
temp$grp <- myfun(temp)
ans <- temp %>%
group_by(grp) %>%
summarise( start = head(start, 1),
stop = tail(stop, 1),
fdate = head(fdate, 1),
ftime = head(ftime, 1),
tdate = tail(tdate, 1),
ttime = tail(ttime, 1)) %>%
ungroup() %>%
select(-grp)
# A tibble: 2 x 6
# start stop fdate ftime tdate ttime
# <fctr> <fctr> <fctr> <fctr> <fctr> <fctr>
# 1 49 171 2017-11-01 13:54:57 2017-11-01 14:23:34
# 2 171 49 2017-11-01 14:57:01 2017-11-01 15:27:30
答案 1 :(得分:1)
这是另一个基本R替代方案:
UIView