Thanks a lot, finally It works! I was wondering why my for() did not work. Now I now the difference between i-3 and (i-3)
Thanks again for all your help. Marlene. > x y vr 1 2 NA 2 1 NA 3 5 NA 4 8 4.333333 5 11 12.333333 6 3 9.000000 7 1 16.333333 8 7 28.000000 9 50 9.333333 10 21 714.333333 11 33 481.000000 12 7 212.333333 13 60 169.333333 2009/9/30 Eik Vettorazzi <e.vettora...@uke.uni-hamburg.de> > 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@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 > [[alternative HTML version deleted]]
______________________________________________ R-help@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.