[R] problem with size of array
jim holtman
jholtman at gmail.com
Mon Apr 28 00:27:08 CEST 2008
Why are you using 'i' in both of the 'for' loops? The value in the
first is overwritten by the second and you will not get the correct
answer.
r <- rnorm(1000,0,1)
caminho <- c(1 , 3.2 , 1.2 , 3, 2, 1, 4, 5)
p2<-function(r){
gama=0
for(i in 1:1000){
c=caminho[[4]]
for(i in 1:caminho[[3]]+1) {
c=c+caminho[[i+3]]*((r[i])^(i-1))
d=(abs(c))*exp(-(x^2/2))}
gama=gama + ( d/(h(r[i]))
)
}
return(gama)
}
e3<-p2(r)
Also why are you using '[[' to access the vector caminho? You sample
program is not complete since 'x' is not defined. Also do you mean
'1:caminho[3]+1' or '1:(caminho[3]+1)' in the second for loop. They
will give you different answers.
So what is the problem you are trying to solve?
On Sun, Apr 27, 2008 at 5:41 PM, Manoel Santos
<manoelsantos666 at gmail.com> wrote:
> + > p2<-function(r){
> + gama=0
> + for(i in 1:1000){
> + c=caminho[[4]]
> + for(i in 1:caminho[[3]]+1) {
> + c=c+caminho[[i+3]]*((r[i])^(i-1))
> + d=(abs(c))*exp(-(x^2/2))}
> + gama=gama + ( d/(h(r[i])) ) }
> + return(gama)}
> > e3<-p2(r)
>
> OBS: r is a rnorm(1000,0,1)
>
> > caminho
> theta_chapeu f_estrela k a0 a1 a2 a3
> 1 3.2 1.2 3 2 1 4 5
> >
>
>
> question
> i wanted gama to be the sum of all.. but i keep getting gama as a array of
> 1000 values...
> ok r doesn't need u to creat variables and correct themm when needed
> but why it corrects gama in this case?
> like gama[1] them gama[2]... gama[1000]
>
> tks guys u are helping me much
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list