我有一个很大的开始结束时间序列数据库,并且想要合并在“ 00:00:00”“结束”(第三列)和在“ 00:00”“开始”(第二列)的所有时间条目:下一行为00”。
当前尝试在末尾正确的地方删除附加的“ 00:00:00”。
#Before
df<-data.frame(id=1,start=c("17/07/2017 19:30","17/07/2017 22:30",
"18/07/2017 00:30","18/07/2017 19:00","18/07/2017 21:30",
"19/07/2017 00:00","19/07/2017 02:30","19/07/2017 21:00",
"19/07/2017 22:30","20/07/2017 00:00",
"20/07/2017 03:00","20/07/2017 22:00","20/07/2017 23:00",
"21/07/2017 00:00","21/07/2017 01:30"),
end=c("17/07/2017 22:00","18/07/2017 00:00","18/07/2017 02:30",
"18/07/2017 21:00","19/07/2017 00:00",
"19/07/2017 02:00","19/07/2017 04:30","19/07/2017 21:30",
"20/07/2017 00:00","20/07/2017 02:00",
"20/07/2017 04:00","20/07/2017 22:30","21/07/2017 00:00",
"21/07/2017 01:00","21/07/2017 02:00"))
#After
dfoutput<-data.frame(id=1,start=c("17/07/2017 19:30","17/07/2017 22:30",
"18/07/2017 00:30","18/07/2017 19:00","18/07/2017 21:30",
"19/07/2017 02:30","19/07/2017 21:00",
"19/07/2017 22:30","20/07/2017 03:00","20/07/2017 22:00",
"20/07/2017 23:00","21/07/2017 01:30"),
end=c("17/07/2017 22:00","18/07/2017 00:00",
"18/07/2017 02:30","18/07/2017 21:00","19/07/2017 02:00",
"19/07/2017 04:30","19/07/2017 21:30",
"20/07/2017 02:00","20/07/2017 04:00","20/07/2017 22:30",
"21/07/2017 01:00","21/07/2017 02:00"))
当前ifelse结果删除正确的最后一行中的“ 00:00:00”。
答案 0 :(得分:1)
我在dplyr
中提出了两种解决方案:
df %>%
group_by(id) %>%
mutate(index = (substr(start,12,16) == "00:00" &
substr(lag(end),12,16) == "00:00"),
start2 = if_else(index, lag(start), start),
end2 = if_else(lead(index), lead(end), end)) %>%
mutate(start = if_else(is.na(start2), start, start2),
end = if_else(is.na(end2), end, end2)) %>%
filter(!index) %>%
select(-index, -start2, -end2)
或
df %>%
group_by(id) %>%
arrange(start, end) %>%
mutate(index1 = ((substr(start,12,16) == "00:00" &
substr(lag(end),12,16) == "00:00") |
(substr(lead(start),12,16) == "00:00" &
substr(end,12,16) == "00:00")),
index2 = cumsum(index1 != c(F, lag(index1)[-1]))) %>%
group_by(id, index2) %>%
mutate(start = if_else(index1, first(start), start),
end = if_else(index1, last(end), end)) %>%
ungroup %>%
select(-index1, -index2) %>%
unique()
都给:
#> # A tibble: 12 x 3
#> id start end
#> <dbl> <fct> <fct>
#> 1 1 17/07/2017 19:30 17/07/2017 22:00
#> 2 1 17/07/2017 22:30 18/07/2017 00:00
#> 3 1 18/07/2017 00:30 18/07/2017 02:30
#> 4 1 18/07/2017 19:00 18/07/2017 21:00
#> 5 1 18/07/2017 21:30 19/07/2017 02:00
#> 6 1 19/07/2017 02:30 19/07/2017 04:30
#> 7 1 19/07/2017 21:00 19/07/2017 21:30
#> 8 1 19/07/2017 22:30 20/07/2017 02:00
#> 9 1 20/07/2017 03:00 20/07/2017 04:00
#> 10 1 20/07/2017 22:00 20/07/2017 22:30
#> 11 1 20/07/2017 23:00 21/07/2017 01:00
#> 12 1 21/07/2017 01:30 21/07/2017 02:00