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,7 +4,8 @@ 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) %>%
filter(battery_status == 2 || battery_status == 3) %>%
@ -17,5 +18,13 @@ consumption <- battery %>%
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)