R/df.rm.R

df.rm<-function(df=NULL, index=NULL, rm="both", frequency=12){
  suppressMessages(require(stats))
  if(is.null(df)){
    stop("Dataframe cannot be omitted.")
  }
  if(rm %in% c("seasonal", "trend", "both")==F){
    stop("Component not removable.")
  }
  if(is.null(index)){
    index<-1:dim(df)[2]
  }
  df.new<-df
  for(i in index){
    components<-decompose(ts(df[,i], frequency=frequency))
    seasonal<-as.numeric(components$seasonal)
    trend<-as.numeric(components$trend)
    trend[is.na(trend)]<-0
    if(rm=="seasonal"){
      df.new[,i]<-df[,i]-seasonal
    }else if(rm=="trend"){
      df.new[,i]<-df[,i]-trend
    }else{
      df.new[,i]<-df[,i]-trend-seasonal
    }
  }
  return(df.new)
}
google-trends-v1/gtm documentation built on June 5, 2019, 5:13 p.m.