Fix phone_sensed_bins when there is no data
parent
06bb2b9c73
commit
47ab0a2ce5
|
@ -17,19 +17,24 @@ for(sensor in all_sensors){
|
||||||
all_sensor_data <- rbind(all_sensor_data, sensor_data)
|
all_sensor_data <- rbind(all_sensor_data, sensor_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
phone_sensed_bins <- all_sensor_data %>%
|
if(nrow(all_sensor_data) == 0){
|
||||||
mutate(bin = (local_minute %/% bin_size) * bin_size) %>% # bin rows into bin_size-minute bins
|
bins = seq(0, 59, by = bin_size)
|
||||||
group_by(local_date, local_hour, bin) %>%
|
hours = seq(0, 23, 1)
|
||||||
summarise(sensor_count = n_distinct(sensor)) %>%
|
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)
|
||||||
ungroup() %>%
|
} else{
|
||||||
mutate(local_date = lubridate::ymd(local_date)) %>%
|
phone_sensed_bins <- all_sensor_data %>%
|
||||||
complete(local_date = seq.Date(min(local_date), max(local_date), by="day"),
|
mutate(bin = (local_minute %/% bin_size) * bin_size) %>% # bin rows into bin_size-minute bins
|
||||||
fill = list(local_hour = 0, bin = 0, sensor_count = 0)) %>%
|
group_by(local_date, local_hour, bin) %>%
|
||||||
complete(nesting(local_date),
|
summarise(sensor_count = n_distinct(sensor)) %>%
|
||||||
local_hour = seq(0, 23, 1),
|
ungroup() %>%
|
||||||
bin = seq(0, (59 %/% bin_size) * bin_size, bin_size),
|
mutate(local_date = lubridate::ymd(local_date)) %>%
|
||||||
fill = list(sensor_count=0)) %>%
|
complete(local_date = seq.Date(min(local_date), max(local_date), by="day"),
|
||||||
pivot_wider(names_from = c(local_hour, bin), values_from = sensor_count)
|
fill = list(local_hour = 0, bin = 0, sensor_count = 0)) %>%
|
||||||
|
complete(nesting(local_date),
|
||||||
write.csv(phone_sensed_bins, output_file, row.names = FALSE)
|
local_hour = seq(0, 23, 1),
|
||||||
|
bin = seq(0, 59, bin_size),
|
||||||
|
fill = list(sensor_count=0)) %>%
|
||||||
|
pivot_wider(names_from = c(local_hour, bin), values_from = sensor_count)
|
||||||
|
|
||||||
|
write.csv(phone_sensed_bins, output_file, row.names = FALSE)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue