# moving from loops to apply

5 messages
Open this post in threaded view
|
Report Content as Inappropriate

## moving from loops to apply

 Dear subscribers, I have made a simulation using loops rather than apply, simply because the loop function seems more natural to me. However, the current simulation takes forever and I have decided - finally - to learn how to use apply, but - as many other people before me - I am having a hard time changing habits. My current problem is: My current code for the loop is: distances <- matrix(NA, 1000, 5) distancer <- function(x, y){-(abs(x-y))} x <- as.matrix(rnorm(1000, 5, 1.67)) y <- rnorm(5, 5, 1.67) for (v in 1:1000){ distances[v,] <- distancer(x[v,], y) } The goal is to calculate the distances between the preferences of each voter (X) and all parties (Y). This gives a 1000 by 5 matrix (distances). If I want to transform this to apply, what would be the best way to go? More specifically, I am not sure what to put into the X part of the apply function. Sorry, for asking this question that is already much debated, I just don't seem to be able to apply to my own case. Many thanks in advance. Kind regards, Gijs Schumacher         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: moving from loops to apply

Open this post in threaded view
|
Report Content as Inappropriate

## Re: moving from loops to apply

Open this post in threaded view
|
Report Content as Inappropriate

## Re: moving from loops to apply

 In reply to this post by Schumacher, G. On Fri, Jun 15, 2012 at 11:27:44AM +0000, Schumacher, G. wrote: > Dear subscribers, > > I have made a simulation using loops rather than apply, simply because the loop function seems more natural to me. However, the current simulation takes forever and I have decided - finally - to learn how to use apply, but - as many other people before me - I am having a hard time changing habits. My current problem is: > > My current code for the loop is: > distances <- matrix(NA, 1000, 5) > distancer <- function(x, y){-(abs(x-y))} > x <- as.matrix(rnorm(1000, 5, 1.67)) > y <- rnorm(5, 5, 1.67) > > for (v in 1:1000){ > distances[v,] <- distancer(x[v,], y) > } > > The goal is to calculate the distances between the preferences of each voter (X) and all parties (Y). This gives a 1000 by 5 matrix (distances). > > If I want to transform this to apply, what would be the best way to go? More specifically, I am not sure what to put into the X part of the apply function. Hi. There are also other ways to eliminate loops than apply-type functions. Try, for example   distances <- matrix(NA, 1000, 5)   distancer <- function(x, y){-(abs(x-y))}   x <- as.matrix(rnorm(1000, 5, 1.67))   y <- rnorm(5, 5, 1.67)     for (v in 1:1000){       distances[v,] <- distancer(x[v,], y)   }     dst1 <- outer(c(x), y, FUN=distancer)     identical(distances, dst1)   [1] TRUE     xm <- matrix(x, nrow=nrow(x), ncol=length(y))   ym <- matrix(y, nrow=nrow(x), ncol=length(y), byrow=TRUE)   dst2 <- -abs(xm - ym)     identical(distances, dst2)   [1] TRUE Apply uses a loop internally, so it need not significantly improve efficiency. Petr Savicky. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.