Dear R-user Suppose I have the following data y=c(2,1,5,8,11,3,1,7,50,21,33,7,60) x=data.frame(y) for(i in 4:nrow(x)) x[i,] =var(x[i-3:i-1,]) I'm trying to get a new variable with the variance of the 3 previous values (just an example) and with NA in the three first positions. I know that my for() is wrong but I'm not able to find my error. Any idea? Thanks, Marlene. [[alternative HTML version deleted]]
I think it should be var(y[i-3:i-1,]) instead of var(x[i-3:i-1,]) otherwise the values of the vector are overwritten Best wishes, Matthias marlene marchena schrieb:> Dear R-user > > Suppose I have the following data > > y=c(2,1,5,8,11,3,1,7,50,21,33,7,60) > > x=data.frame(y) > > for(i in 4:nrow(x)) x[i,] =var(x[i-3:i-1,]) > > I'm trying to get a new variable with the variance of the 3 previous values > (just an example) and with NA in the three first positions. I know that my > for() is wrong > but I'm not able to find my error. > > Any idea? > > Thanks, > > Marlene. > > [[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. > >-- Dr. rer. nat. Matthias Gondan Institut f?r Psychologie Universit?t Regensburg D-93050 Regensburg Tel. 0941-943-3856 Fax 0941-943-3233 Email: matthias.gondan at psychologie.uni-regensburg.de http://www.psychologie.uni-r.de/Greenlee/team/gondan/gondan.html
Hi Try function rollapply from zoo.It can compute rolling results of functions. Make your y a zoo object and use rollapply(object, 3, var) Regards Petr r-help-bounces at r-project.org napsal dne 30.09.2009 12:37:36:> Dear R-user > > Suppose I have the following data > > y=c(2,1,5,8,11,3,1,7,50,21,33,7,60) > > x=data.frame(y) > > for(i in 4:nrow(x)) x[i,] =var(x[i-3:i-1,]) > > I'm trying to get a new variable with the variance of the 3 previousvalues> (just an example) and with NA in the three first positions. I know thatmy> for() is wrong > but I'm not able to find my error. > > Any idea? > > Thanks, > > Marlene. > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Marlene, x=data.frame(y,vr=NA) for(i in 4:nrow(x)) x[i,"vr"] =var(y[(i-3):(i-1)]) will do the trick, solving both problems with overwriting subsequent x-values and obeying the precedence of R operators. For the latter, see ?Syntax and compare i<-5 i-3:i-1 (i-3):(i-1) hth. marlene marchena schrieb:> Dear R-user > > Suppose I have the following data > > y=c(2,1,5,8,11,3,1,7,50,21,33,7,60) > > x=data.frame(y) > > for(i in 4:nrow(x)) x[i,] =var(x[i-3:i-1,]) > > I'm trying to get a new variable with the variance of the 3 previous values > (just an example) and with NA in the three first positions. I know that my > for() is wrong > but I'm not able to find my error. > > Any idea? > > Thanks, > > Marlene. > > [[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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf K?rperschaft des ?ffentlichen Rechts Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. J?rg F. Debatin (Vorsitzender) Dr. Alexander Kirstein Ricarda Klein Prof. Dr. Dr. Uwe Koch-Gromus