[R] [plyr] Moving average filter with plyr

arun smartpink111 at yahoo.com
Tue Aug 27 16:50:52 CEST 2013


If you look at:

str(lst1)
List of 10
 $ 1.1 :'data.frame':    783 obs. of  6 variables:
  ..$ Time     : num [1:783] 1.83e-05 2.37e-05 2.90e-05 5.06e-04 5.11e-04 ...
  ..$ ChannelA : num [1:783] 4.53 4.88 4.8 3.58 3.58 ...
  ..$ ChannelB : num [1:783] 6.87 13.6 21.09 22.09 21.55 ...
  ..$ FileNo   : num [1:783] 1 1 1 1 1 1 1 1 1 1 ...
  ..$ ChannelNo: num [1:783] 1 1 1 1 1 1 1 1 1 1 ...
  ..$ filtered : Time-Series [1:783] from 1 to 783: NA NA 17 19.8 21.2 ... #returns a time-series.

You might have to change the function:
applyfilter<-function(x){
   return(as.numeric(filter(x,rep(1/5, times=5))))
 }
 data.trash<-ddply(data, .(FileNo, ChannelNo), transform, filtered=applyfilter(ChannelB))
 head(data.trash)
          Time ChannelA  ChannelB FileNo ChannelNo filtered
1 0.0000183214 4.527559  6.873434      1         1       NA
2 0.0000236814 4.881890 13.602919      1         1       NA
3 0.0000290414 4.803150 21.092224      1         1 17.04348
4 0.0005060814 3.582677 22.094999      1         1 19.84974
5 0.0005114414 3.582677 21.553814      1         1 21.21485
6 0.0005168014 3.582677 20.904732      1         1 20.96478


A.K.



----- Original Message -----
From: Ingo Reinhold <ingor at kth.se>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, August 27, 2013 10:32 AM
Subject: RE: [R] [plyr] Moving average filter with plyr

Thank you very much. 

But why is this? The function applyfilter should return a numeric result, shouldn't it?

Best regards, 

Ingo
________________________________________
From: arun [smartpink111 at yahoo.com]
Sent: Tuesday, August 27, 2013 4:06 PM
To: Ingo Reinhold
Cc: R help
Subject: Re: [R] [plyr] Moving average filter with plyr

#or
data2<-ddply(data,.(FileNo,ChannelNo),transform,filtered=as.numeric(applyfilter(ChannelB)))
A.K.




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Ingo Reinhold <ingor at kth.se>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, August 27, 2013 10:03 AM
Subject: Re: [R] [plyr] Moving average filter with plyr

Hi,
May be this helps:


lst1<-dlply(data,.(FileNo,ChannelNo),transform,filtered=applyfilter(ChannelB))
data2<-do.call(rbind,lapply(lst1,function(x){x$filtered<- as.numeric(x$filtered);x}))
row.names(data2)<- row.names(data)
head(data,3)
#            Time ChannelA  ChannelB FileNo ChannelNo
#1161 1.83214e-05 4.527559  6.873434      1         1
#1171 2.36814e-05 4.881890 13.602919      1         1
#1181 2.90414e-05 4.803150 21.092224      1         1
head(data2,3)
#            Time ChannelA  ChannelB FileNo ChannelNo filtered
#1161 1.83214e-05 4.527559  6.873434      1         1       NA
#1171 2.36814e-05 4.881890 13.602919      1         1       NA
#1181 2.90414e-05 4.803150 21.092224      1         1 17.04348
A.K.



----- Original Message -----
From: Ingo Reinhold <ingor at kth.se>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Tuesday, August 27, 2013 9:17 AM
Subject: [R] [plyr] Moving average filter with plyr

Dear all,

I'm stuck with a problem using plyr to process a rather large junk of data. What I'm trying to do is applying a moving average to all the subparts of the dataframe (the example data can be found here https://dl.dropboxusercontent.com/u/2414056/testData.Rdata).

require(plyr)

load("testData.Rdata")

applyfilter<-function(x){
  return(filter(x,rep(1/5, times=5)))
}
data.trash<-ddply(data, .(FileNo, ChannelNo), transform, filtered=applyfilter(ChannelB))



The result from this is

Error in attributes(output[[var]]) <- attributes(value) : invalid time series parameters specified

though the number of return values seems right to me.

Do you have any thoughts or suggestions?

Many thanks,

Ingo



    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list