[R] mean over previous cells

2009-02-20 Thread clion
Dear RUsers, I guess this is an easy question for someone a little familiar with programming...(which I am not)... I've got 2 colummns, one shows just dates(SST_date, Class 'Date' num), the other one shows the SeaSurfaceTemperature (SST, num) at that certain date. SST_dateSST 2008-01-01 22.

Re: [R] mean over previous cells

2009-02-20 Thread lauramorg...@bluewin.ch
If, suppose, the name of your dataframe is "dataframe" , you could try something like: mean60days<-c() for(i in 1:length(dataframe$SST)) + { + mean60days[[i]]<-mean(dataframe$SST[i-10:i]) + } I'm not really sure it will work... my skills aren't that great... Anyway, it worked on a sample I tried i

Re: [R] mean over previous cells

2009-02-20 Thread lauramorg...@bluewin.ch
Sorry, I meant: If, suppose, the name of your dataframe is "dataframe" , you could try something like: mean60days<-c() for(i in 1:length(dataframe$SST)) + { + mean60days[[i]]<-mean(dataframe$SST[i-60:i]) + } I'm not really sure it will work... my skills aren't that great... Anyway, it worked on a

Re: [R] mean over previous cells

2009-02-20 Thread clion
well, it "kind of works" now, I get means from the 61st SST-value on (which is reasonable, so no "carful discarding" of the first 60 values) but aswell I only get means until the 186st SST-value (although I 've got about 4900 in total). Allt the rest is "NA" - besides the 60th value is "NaN" Any

Re: [R] mean over previous cells

2009-02-20 Thread Gabor Grothendieck
Using the zoo package: Lines <- "SST_dateSST 2008-01-01 22.2 2008-01-02 21.8 2008-01-03 22.8 2008-01-04 22.9 2008-01-05 23.1 2008-01-06 23.2" library(zoo) # z <- read.zoo("myfile.dat", header = TRUE) z <- read.zoo(textConnection(Lines), header = TRUE) merge(z, avg3 = rollmean(z, 3, align = "