[R] Help with "apply"

Berend Hasselman bhh at xs4all.nl
Fri May 13 08:10:23 CEST 2011


M.Ribeiro wrote:
> 
> Ok, I am sorry, I was building a reproducible example to post here and I
> made a mistake. Th second loop (in the variable k) updates ycorr.
> Therefore, it would be
> 
> ycorr <- rnorm(10)
> Nindiv <- 10
> x <-matrix(sample(c(0:2),50,re = T),10)
> gtemp <- rnorm(10)
> for (i in 1:Nindiv) {
>         for (k in 1:ncol(x)) {
>                 ycorr[i] <- ycorr[i] - x[i,k]*gtemp[k]
>         }
> }
> 

Why does gtemp have 10 elements? You are only using the first 5 in the loop.

Instead of the complete loop try this:  ycorr <-  ycorr - x %*%
gtemp[1:ncol(x)]

Berend

--
View this message in context: http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3519394.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list