2020-06-24 21:46:27 +02:00
|
|
|
library(dplyr)
|
|
|
|
|
|
|
|
filter_by_day_segment <- function(data, day_segment) {
|
|
|
|
if(day_segment %in% c("morning", "afternoon", "evening", "night"))
|
|
|
|
data <- data %>% filter(local_day_segment == day_segment)
|
|
|
|
|
|
|
|
return(data %>% group_by(local_date))
|
|
|
|
}
|
|
|
|
|
|
|
|
compute_wifi_feature <- function(data, feature, day_segment){
|
2020-07-07 23:15:32 +02:00
|
|
|
data <- data %>% filter_by_day_segment(day_segment)
|
2020-06-24 21:46:27 +02:00
|
|
|
if(feature %in% c("countscans", "uniquedevices")){
|
|
|
|
data <- switch(feature,
|
|
|
|
"countscans" = data %>% summarise(!!paste("wifi", day_segment, feature, sep = "_") := n()),
|
|
|
|
"uniquedevices" = data %>% summarise(!!paste("wifi", day_segment, feature, sep = "_") := n_distinct(bssid)))
|
|
|
|
return(data)
|
|
|
|
} else if(feature == "countscansmostuniquedevice"){
|
|
|
|
# Get the most scanned device
|
2020-07-07 23:15:32 +02:00
|
|
|
mostuniquedevice <- data %>%
|
|
|
|
group_by(bssid) %>%
|
2020-06-24 21:46:27 +02:00
|
|
|
mutate(N=n()) %>%
|
|
|
|
ungroup() %>%
|
2020-07-07 23:15:32 +02:00
|
|
|
filter(N == max(N)) %>%
|
|
|
|
head(1) %>% # if there are multiple device with the same amount of scans pick the first one only
|
|
|
|
pull(bssid)
|
2020-06-24 21:46:27 +02:00
|
|
|
return(data %>%
|
2020-07-07 23:15:32 +02:00
|
|
|
filter(bssid == mostuniquedevice) %>%
|
|
|
|
group_by(local_date) %>%
|
|
|
|
summarise(!!paste("wifi", day_segment, feature, sep = "_") := n()) %>%
|
|
|
|
replace(is.na(.), 0))
|
2020-06-24 21:46:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base_wifi_features <- function(wifi_data, day_segment, requested_features){
|
|
|
|
# Output dataframe
|
|
|
|
features = data.frame(local_date = character(), stringsAsFactors = FALSE)
|
|
|
|
|
|
|
|
# The name of the features this function can compute
|
|
|
|
base_features_names <- c("countscans", "uniquedevices", "countscansmostuniquedevice")
|
|
|
|
|
|
|
|
# The subset of requested features this function can compute
|
|
|
|
features_to_compute <- intersect(base_features_names, requested_features)
|
|
|
|
|
|
|
|
for(feature_name in features_to_compute){
|
|
|
|
feature <- compute_wifi_feature(wifi_data, feature_name, day_segment)
|
|
|
|
features <- merge(features, feature, by="local_date", all = TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
return(features)
|
|
|
|
}
|