rapids/src/features/battery_deltas.R

35 lines
1.5 KiB
R
Raw Normal View History

2020-05-02 01:46:04 +02:00
source("renv/activate.R")
2019-11-05 16:47:55 +01:00
library("tidyverse")
battery <- read.csv(snakemake@input[[1]])
if(nrow(battery) > 0){
consumption <- battery %>%
2019-11-05 16:47:55 +01:00
mutate(group = ifelse(lag(battery_status) != battery_status, 1, 0) %>% coalesce(0),
group_id = cumsum(group) + 1) %>%
filter(battery_status == 2 | battery_status == 3) %>%
2019-11-05 16:47:55 +01:00
group_by(group_id) %>%
summarize(battery_diff = first(battery_level) - last(battery_level),
time_diff = (last(timestamp) - first(timestamp)) / (1000 * 60 * 60),
local_start_date_time = first(local_date_time),
local_end_date_time = last(local_date_time),
local_start_date = first(local_date),
local_end_date = last(local_date),
local_start_day_segment = first(local_day_segment),
local_end_day_segment = last(local_day_segment)) %>%
2019-11-05 16:47:55 +01:00
select(-group_id) %>%
2019-11-05 17:35:01 +01:00
filter(time_diff > 0.1) # Avoids including quick cycles
} else {
consumption <- data.frame(battery_diff = numeric(),
time_diff = numeric(),
local_start_date_time = character(),
local_end_date_time = character(),
local_start_date = character(),
local_end_date = character(),
local_start_day_segment = character(),
local_end_day_segment = character())
}
2019-11-05 16:47:55 +01:00
write.csv(consumption, snakemake@output[[1]], row.names = FALSE)