2020-06-24 21:10:42 +02:00
|
|
|
library(dplyr)
|
|
|
|
library(tidyr)
|
|
|
|
|
|
|
|
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_bluetooth_feature <- function(data, feature, day_segment){
|
2020-07-07 00:11:28 +02:00
|
|
|
data <- data %>% filter_by_day_segment(day_segment)
|
2020-06-24 21:10:42 +02:00
|
|
|
if(feature %in% c("countscans", "uniquedevices")){
|
|
|
|
data <- switch(feature,
|
|
|
|
"countscans" = data %>% summarise(!!paste("bluetooth", day_segment, feature, sep = "_") := n()),
|
|
|
|
"uniquedevices" = data %>% summarise(!!paste("bluetooth", day_segment, feature, sep = "_") := n_distinct(bt_address)))
|
|
|
|
return(data)
|
|
|
|
} else if(feature == "countscansmostuniquedevice"){
|
|
|
|
# Get the most scanned device
|
2020-07-07 00:11:28 +02:00
|
|
|
mostuniquedevice <- data %>%
|
|
|
|
group_by(bt_address) %>%
|
2020-06-24 21:10:42 +02:00
|
|
|
mutate(N=n()) %>%
|
|
|
|
ungroup() %>%
|
2020-07-07 00:11:28 +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(bt_address)
|
2020-06-24 21:10:42 +02:00
|
|
|
return(data %>%
|
2020-07-07 00:11:28 +02:00
|
|
|
filter(bt_address == mostuniquedevice) %>%
|
|
|
|
group_by(local_date) %>%
|
|
|
|
summarise(!!paste("bluetooth", day_segment, feature, sep = "_") := n()) %>%
|
|
|
|
replace(is.na(.), 0))
|
2020-06-24 21:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base_bluetooth_features <- function(bluetooth_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_bluetooth_feature(bluetooth_data, feature_name, day_segment)
|
|
|
|
features <- merge(features, feature, by="local_date", all = TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
features <- features %>% mutate_at(vars(contains("countscansmostuniquedevice")), list( ~ replace_na(., 0)))
|
|
|
|
|
|
|
|
return(features)
|
|
|
|
}
|