Update battery_deltas.R to handle empty dataframes

replace/c47cd97b672aaa7bda398da21748dea84d50772b
JulioV 2019-11-13 14:53:28 -05:00
parent d7536d5cb5
commit d2e9b79af6
1 changed files with 16 additions and 7 deletions

View File

@ -4,18 +4,27 @@ library("tidyverse")
battery <- read.csv(snakemake@input[[1]])
consumption <- battery %>%
if(nrow(battery) > 0){
consumption <- battery %>%
mutate(group = ifelse(lag(battery_status) != battery_status, 1, 0) %>% coalesce(0),
group_id = cumsum(group) + 1) %>%
group_id = cumsum(group) + 1) %>%
filter(battery_status == 2 || battery_status == 3) %>%
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)) %>%
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)) %>%
select(-group_id) %>%
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())
}
write.csv(consumption, snakemake@output[[1]], row.names = FALSE)