Fix phone_sensed_bins when there is no data

pull/104/head
JulioV 2020-09-21 10:28:42 -04:00
parent 06bb2b9c73
commit 47ab0a2ce5
1 changed files with 20 additions and 15 deletions

View File

@ -17,6 +17,11 @@ for(sensor in all_sensors){
all_sensor_data <- rbind(all_sensor_data, sensor_data) all_sensor_data <- rbind(all_sensor_data, sensor_data)
} }
if(nrow(all_sensor_data) == 0){
bins = seq(0, 59, by = bin_size)
hours = seq(0, 23, 1)
write.csv(crossing(hours, bins) %>% unite("hour_bin",hours, bins, sep = "_") %>% mutate(value = NA, local_date = NA) %>% pivot_wider(names_from = hour_bin, values_from=value) %>% head(0), output_file, row.names = FALSE)
} else{
phone_sensed_bins <- all_sensor_data %>% phone_sensed_bins <- all_sensor_data %>%
mutate(bin = (local_minute %/% bin_size) * bin_size) %>% # bin rows into bin_size-minute bins mutate(bin = (local_minute %/% bin_size) * bin_size) %>% # bin rows into bin_size-minute bins
group_by(local_date, local_hour, bin) %>% group_by(local_date, local_hour, bin) %>%
@ -27,9 +32,9 @@ phone_sensed_bins <- all_sensor_data %>%
fill = list(local_hour = 0, bin = 0, sensor_count = 0)) %>% fill = list(local_hour = 0, bin = 0, sensor_count = 0)) %>%
complete(nesting(local_date), complete(nesting(local_date),
local_hour = seq(0, 23, 1), local_hour = seq(0, 23, 1),
bin = seq(0, (59 %/% bin_size) * bin_size, bin_size), bin = seq(0, 59, bin_size),
fill = list(sensor_count=0)) %>% fill = list(sensor_count=0)) %>%
pivot_wider(names_from = c(local_hour, bin), values_from = sensor_count) pivot_wider(names_from = c(local_hour, bin), values_from = sensor_count)
write.csv(phone_sensed_bins, output_file, row.names = FALSE) write.csv(phone_sensed_bins, output_file, row.names = FALSE)
}