我想知道如何使用一個條件來計算每天的總小時數,即只有在第三列中分配了 X 的時間應該是總和。我有這個 data.frame:
| 日期 | 時間 | 跑 |
|---|---|---|
| 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 | |
| 17/04/12 | 00:29:00 | X |
| 17/04/12 | 00:30:00 | X |
| 18/04/12 | 00:10:00 | |
| 18/04/12 | 00:14:00 | X |
| 18/04/12 | 00:20:00 | X |
結果:
| 日期 | 總時間 |
|---|---|
| 17/04/12 | 00:20:00 |
| 18/04/12 | 00:06:00 |
我嘗試使用 lubridate 和 tidyverse 軟體包,但沒有成功。
謝謝
uj5u.com熱心網友回復:
我們可以使用運行長度編碼 (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 包(v2.0.1)于 2021 年 10 月 7 日創建
uj5u.com熱心網友回復:
我不確定這是否是您想要做的...
library(lubridate)
library(dplyr)
df %>%
mutate(
Date = dmy(Date),
Time = hms(Time),
# minute(Time)
) %>%
filter(Run == "x") %>%
group_by(Date) %>%
summarise(Total_Time = seconds_to_period(sum(period_to_seconds(Time))))
# A tibble: 2 x 2
Date Total_Time
<date> <Period>
1 2012-04-17 1H 39M 0S
2 2012-04-18 34M 0S
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313690.html
下一篇:將作業日添加到df列
