Skip to content

Instantly share code, notes, and snippets.

@nk773
Created October 6, 2016 23:45
Show Gist options
  • Save nk773/47f9c1e9512b09203b7d32dbb5167da0 to your computer and use it in GitHub Desktop.
Save nk773/47f9c1e9512b09203b7d32dbb5167da0 to your computer and use it in GitHub Desktop.
This function finds the moving average and standard deviation within the dataset and adds them as new columns within the dataset
addMeanFeature<-function(dataset1, colrng, bycol=NA, windowsize){
library(zoo)
id<-bycol
#print(paste("id =", id))
rollingmean = c()
rollingsd = c()
rng <- which(names(dataset1)%in%colrng)
a = paste("a",(1:length(rng)),sep="") # average
sd =paste("sd",(1:length(rng)),sep="") # standard deviation
if (!is.na(id)) {
#print(length(id))
if (length(id)>1){
id<-id[1]
}
if (is.numeric(id)) {
if (id>length(names(dataset1))){
id <- 0
}
}
}
if((!is.na(id)) &(id!=0)) {
idxcol <- unique(dataset1[[id]])
nid <- length(idxcol)
for (i in seq(1:nid)) {
sub_data = subset(dataset1[,rng], dataset1[[id]] == idxcol[i])
n_row_subdata = nrow(sub_data)
w=ifelse(windowsize < n_row_subdata,windowsize,n_row_subdata)
# get the rolling mean for all sensors
rollingmean = rbind(rollingmean,rollapply(sub_data,w,mean,align = "right",partial=1))
# get the rolling sd for all sensors
rollingsd_i = rollapply(sub_data,w,sd,align = "right",partial=1)
rollingsd_i[is.na(rollingsd_i)]=0
rollingsd = rbind(rollingsd,rollingsd_i)
}
}
else {
#print(rng)
sub_data <- dataset1[,rng]
#print(sub_data[1:3,])
n_row_subdata = nrow(sub_data)
#print(n_row_subdata)
w=ifelse(windowsize < n_row_subdata,windowsize,n_row_subdata)
#print (w)
# get the rolling mean for all sensors
rollingmean = rbind(rollingmean,rollapply(sub_data,w,mean,align = "right",partial=1))
# get the rolling sd for all sensors
rollingsd_i = rollapply(sub_data,w,sd,align = "right",partial=1)
rollingsd_i[is.na(rollingsd_i)]=0
rollingsd = rbind(rollingsd,rollingsd_i)
}
data_a = as.data.frame(rollingmean)
data_sd = as.data.frame(rollingsd)
names(data_a) = a
names(data_sd) = sd
df = cbind(data_a,data_sd)
df2=cbind(dataset1,df)
return (df2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment