rapids/src/features/phone_bluetooth/rapids/main.R

52 lines
2.0 KiB
R
Raw Normal View History

library("dplyr", warn.conflicts = F)
2020-06-24 21:10:42 +02:00
library(tidyr)
compute_bluetooth_feature <- function(data, feature, day_segment){
data <- data %>% filter_data_by_segment(day_segment)
2020-06-24 21:10:42 +02:00
if(feature %in% c("countscans", "uniquedevices")){
data <- data %>% group_by(local_segment)
2020-06-24 21:10:42 +02:00
data <- switch(feature,
"countscans" = data %>% summarise(!!feature := n()),
"uniquedevices" = data %>% summarise(!!feature := n_distinct(bt_address)))
2020-06-24 21:10:42 +02:00
return(data)
} else if(feature == "countscansmostuniquedevice"){
# Get the most scanned device
mostuniquedevice <- data %>%
group_by(bt_address) %>%
2020-06-24 21:10:42 +02:00
mutate(N=n()) %>%
ungroup() %>%
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)
mostuniquedevice
2020-06-24 21:10:42 +02:00
return(data %>%
filter(bt_address == mostuniquedevice) %>%
group_by(local_segment) %>%
summarise(!!feature := n()) %>%
replace(is.na(.), 0))
2020-06-24 21:10:42 +02:00
}
}
rapids_features <- function(sensor_data_files, day_segment, provider){
bluetooth_data <- read.csv(sensor_data_files[["sensor_data"]], stringsAsFactors = FALSE)
requested_features <- provider[["FEATURES"]]
# Output dataframe
features = data.frame(local_segment = character(), stringsAsFactors = FALSE)
2020-06-24 21:10:42 +02:00
# The name of the features this function can compute
base_features_names <- c("countscans", "uniquedevices", "countscansmostuniquedevice")
2020-06-24 21:10:42 +02:00
# The subset of requested features this function can compute
features_to_compute <- intersect(base_features_names, requested_features)
2020-06-24 21:10:42 +02:00
for(feature_name in features_to_compute){
feature <- compute_bluetooth_feature(bluetooth_data, feature_name, day_segment)
features <- merge(features, feature, by="local_segment", all = TRUE)
}
2020-06-24 21:10:42 +02:00
2020-11-25 22:34:05 +01:00
features <- features %>% mutate_at(vars(contains("countscansmostuniquedevice")), list( ~ replace_na(., 0)))
2020-06-24 21:10:42 +02:00
return(features)
2020-06-24 21:10:42 +02:00
}