R/f_temporal_aggregation_R.R

Defines functions temporal_aggregation

Documented in temporal_aggregation

# temporal aggregation --------------------------------------------------------

#' Aggregate values in time
#' @export
#' @param type Sensor type
#' @param type_sub Number of sensors considered 
#' @param interpolation Interpolation type
#' @param adjustment Type of adjustment 
#' @param adjustment_sub Subtype of adjustment
#' @param time Temporal aggregation
temporal_aggregation <- function(type,type_sub,interpolation,adjustment,adjustment_sub,time) {
  
  if(type=="") stop("Warning: choose measurement type")
  
  if(time != ""){
  
    if(time == "_10min"){
      time <- "10 min"
    }
    if(time == "_5min"){
      time <- "5 min"
    }
    if(time == "_1h"){
      time <- "1 hour"
    }
    
    for(intr in 1:35){
      
      assign("tmp_data",get(load(file = paste0(baseLoc,"/../data-processed/",type,"/",type,type_sub,interpolation,adjustment,adjustment_sub,"_event_",intr,".RData"))))
      
      raw_col_names <- colnames(tmp_data)[-1]
      Date <- as.POSIXct(as.numeric(raw_col_names),format="%Y-%m-%d %H:%M:%S",origin=strptime("1970-01-01 00:00:00", format = "%Y-%m-%d %H:%M:%S"))
      
      tmp <- as.data.frame(t(as.matrix(tmp_data[-1])))
  
      tmp_df <- data.frame(Date=Date,unname(tmp))
        
      tmp_df <- dplyr::summarise_all(dplyr::group_by(tmp_df,Date = cut(Date, time)), dplyr::funs(mean), na.rm = T)
      
      Date_agg <- tmp_df$Date
      
      tmp_df <- as.data.frame(t(as.matrix(tmp_df[-1])))
      
      tmp_df <- cbind(tmp_data$ID,tmp_df,stringsAsFactors=F)
      
      colnames(tmp_df) <- c("ID",as.numeric(as.POSIXct(Date_agg)))
      
      save(tmp_df,file=paste0(baseLoc,"/../data-processed/",type,"/",key_id,"_event_",intr,".RData"))
      
    }
  }
}
adisch87/COMCORDEcml documentation built on Dec. 22, 2017, 11:11 a.m.