0

I will try to be as less vague as possible. The below data set consists of a device's power measurement and I have to plot a graph which would show the average fluctuation of the power (watt) during the Time column. I have to accomplish this in R but i really don't know which function or how should I do it as i'm a newbie to R. Any help will be highly appreciated!

Store No.,Date,Time,Watt

33,2011/09/26,09:11:01,0.0599E+03

34,2011/09/26,09:11:02,0.0597E+03

35,2011/09/26,09:11:03,0.0598E+03

36,2011/09/26,09:11:04,0.0596E+03

37,2011/09/26,09:11:05,0.0593E+03

38,2011/09/26,09:11:06,0.0595E+03

39,2011/09/26,09:11:07,0.0595E+03

40,2011/09/26,09:11:08,0.0595E+03

41,2011/09/26,09:11:09,0.0591E+03
joran
  • 169,992
  • 32
  • 429
  • 468
Amaar Bokhari
  • 169
  • 4
  • 11
  • The average would be a single number, so it would be a pretty boring plot. So I think you want something different, or I misunderstand, or both. Maybe show an example of the data (numeric) you want to plot, given the input above? – Ed Staub Sep 30 '11 at 16:33
  • something like this, http://www.sr.bham.ac.uk/~ajrs/R/gallery/midday_weather_profiles.png in the picture, they have plotted the raw data and the single like is representing the fluctuation (or i have misunderstood the meaning of the line:)) – Amaar Bokhari Sep 30 '11 at 16:50
  • Ah - ok, that's called a _moving_ average, or, more generically, a low-pass filter. I'm a newbie too, so beyond helping with the question, I can't help! – Ed Staub Sep 30 '11 at 16:52
  • i see...any idea that how can it be done in R?(code wise) – Amaar Bokhari Sep 30 '11 at 16:54
  • 2
    I could google for it, but then you can too! – Ed Staub Sep 30 '11 at 16:58
  • Yeah im doing that...let's hope for the best! – Amaar Bokhari Sep 30 '11 at 16:59
  • See answers to [this related/duplicate question][1]. [1]: http://stackoverflow.com/questions/743812/calculating-moving-average-in-r – Ed Staub Sep 30 '11 at 17:11

1 Answers1

0

rollapply in package:zoo will return a moving average (or a moving any-function). You can plot using points and then add a moving average line:

dat$D.time <- as.POSIXct(paste(dat$Date, dat$Time))
require(zoo)
 ?rollapply
 length(rollapply(dat$Watt,3, mean))
plot(dat$D.time, dat$Watt)
lines(dat$D.time[3:9], rollapply(dat$Watt,3, mean))
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • It gives me an error when i execute the line statement. The error is; "Error in xy.coords(x, y) : 'x' and 'y' lengths differ" – Amaar Bokhari Oct 01 '11 at 10:21
  • After tweaking, it finally worked!Since i was applying the code on a bigger set,values in the line statement had to be changed!Thanks 'Dwin'! – Amaar Bokhari Oct 01 '11 at 11:16