Hello R gurus,
I have a simple routine using for loops that i was just trying to make it
faster
y <- rnorm(10)
Nindiv <- 10
x <-matrix(sample(c(0:2),50,re = T),10)
gtemp <- rnorm(10)
ycorr <- array(0,c(Nindiv,1))
for (i in 1:Nindiv) {
for (k in 1:ncol(x)) {
ycorr[i] <- y[i] - x[i,k]*gtemp[k]
}
}
It works fine but I wanted to make it faster and I think an apply function
could probably solve this problem.
Any suggestions? Thanks
--
View this message in context:
http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3518773.html
Sent from the R help mailing list archive at Nabble.com.
Hi: Are you *sure* that's what you want to do?> y - x[,5] * gtemp[5][1] 0.10116205 2.46220119 3.04966544 0.56074569 -1.14872521 -0.89275228 [7] 3.52161381 0.64880894 0.70167063 -0.06120996> identical(as.vector(ycorr), y - x[,5] * gtemp[5])[1] TRUE Perhaps you ought to explain what you are trying to compute, because your inner loop is returning y[i] - x[, 5] * gtemp[5] for each i, where 5 = ncol(x). Dennis On Thu, May 12, 2011 at 2:23 PM, M.Ribeiro <mresendeufv at yahoo.com.br> wrote:> Hello R gurus, > > I have a simple routine using for loops that i was just trying to make it > faster > > y <- rnorm(10) > Nindiv <- 10 > x <-matrix(sample(c(0:2),50,re = T),10) > gtemp <- rnorm(10) > ycorr <- array(0,c(Nindiv,1)) > for (i in 1:Nindiv) { > ? ? ? ?for (k in 1:ncol(x)) { > ? ? ? ? ? ? ? ?ycorr[i] <- y[i] - x[i,k]*gtemp[k] > ? ? ? ?} > } > > It works fine but I wanted to make it faster and I think an apply function > could probably solve this problem. > Any suggestions? Thanks > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3518773.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
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]
}
}
--
View this message in context:
http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3519270.html
Sent from the R help mailing list archive at Nabble.com.
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.