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.33
5 11 12.33
6 3 9.00
7 1 16.
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
marlene marchena napsal dne 30.09.2009
13:28:16:
> Hi Petr,
>
> Thanks for your suggestion. It woks, but now I have other problem the
> positions of the values changed. I need the NA values in the three first
positions.
>
> y=c(2,1,5,8,11,3,1,7,50,21,33,7,60)
> x=as.zoo(y)
> > x
> 1 2 3
Hi Petr,
Thanks for your suggestion. It woks, but now I have other problem the
positions of the values changed. I need the NA values in the three first
positions.
y=c(2,1,5,8,11,3,1,7,50,21,33,7,60)
x=as.zoo(y)
> x
1 2 3 4 5 6 7 8 9 10 11 12 13
2 1 5 8 11 3 1 7 50 21 33 7 60
va
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)
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
6 matches
Mail list logo