[R] whole object approach for nested loops

From: T C <bflat24_at_gmail.com>
Date: Tue 12 Sep 2006 - 19:51:35 GMT

I have the following code that I am trying to execute using the whole object approach and get rid of the for loop. I have looked at the manual and seached the database for examples or similar questions with no luck. The following example works without any problems.

for (j in 1:186)
entropy.cogp[1:30000, j]<-alpha3[1:30000]*c[j,2] }

But when I try to remove the for loop and use

entropy.cogp[1:30000, 1:186]<-alpha3[1:30000]*c[1:186,2]

R tries to multiply the first member of alpha3 with the first member of c[,2] and once c is exhausted, it multiplies the 187th member of alpha3 with the first member of c[,2] and so on, resulting in an error where it requires the size of alpha3 to be an exact multiple of the size of c. This is clearly not what is intended by the for loop given above. Is there a way to do this using a whole object approach to make things run faster? Or is the for loop the only way of doing this?


R-help@stat.math.ethz.ch 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. Received on Wed Sep 13 05:55:40 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 12 Sep 2006 - 20:31:02 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.