我想知道如何使用只有在第三列中分配有 X 的时间才能求和的条件来求和每天的总小时数。我有这个数据框:
结果:
我尝试使用 lubridate 和 tidyverse 软件包,但没有成功。
谢谢
最佳答案
我们可以使用游程编码(rle)将时间点划分为跳跃。然后,我们可以总结跳跃的持续时间:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
#' Run length encoding to output leap numbers
#' AAABBA => 111223
encode_leap <- function(x) {
rle <- rle(x)
rle$values <- rle$values %>%
length() %>%
seq()
inverse.rle(rle)
}
data <- tribble(
~Date, ~Time, ~Run,
"17/04/12", "00:10:00", "x",
"17/04/12", "00:19:00", "x",
"17/04/12", "00:25:00", "x",
"17/04/12", "00:29:00", "x",
"17/04/12", "00:25:00", NA,
"17/04/12", "00:29:00", "x",
"17/04/12", "00:30:00", "x",
"18/04/12", "00:10:00", NA,
"18/04/12", "00:14:00", "x",
"18/04/12", "00:20:00", "x"
)
data
#> # A tibble: 10 x 3
#> Date Time Run
#> <chr> <chr> <chr>
#> 1 17/04/12 00:10:00 x
#> 2 17/04/12 00:19:00 x
#> 3 17/04/12 00:25:00 x
#> 4 17/04/12 00:29:00 x
#> 5 17/04/12 00:25:00 <NA>
#> 6 17/04/12 00:29:00 x
#> 7 17/04/12 00:30:00 x
#> 8 18/04/12 00:10:00 <NA>
#> 9 18/04/12 00:14:00 x
#> 10 18/04/12 00:20:00 x
data %>%
mutate(date = Date) %>%
# make year to 4 digits
unite(Date, Time, col = "datetime", sep = "00 - ") %>%
mutate(
date,
datetime = datetime %>% parse_datetime(format = "%d/%M/%Y - %H:%M:%S"),
leap = encode_leap(.$Run)
) %>%
group_by(date, leap) %>%
summarise(duration = max(datetime) - min(datetime)) %>%
group_by(date) %>%
summarise(duration = sum(duration, na.rm = TRUE))
#> `summarise()` has grouped output by 'date'. You can override using the `.groups` argument.
#> # A tibble: 2 x 2
#> date duration
#> <chr> <drtn>
#> 1 17/04/12 1200 secs
#> 2 18/04/12 360 secs
由 reprex package 于 2021 年 10 月 7 日创建(v2.0.1)
关于r - R 中按日期列出的总小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69480292/