[R] apply question

David Winsemius dwinsemius at comcast.net
Fri Apr 30 23:00:33 CEST 2010


On Apr 30, 2010, at 4:52 PM, Giovanni Azua wrote:

> Hello,
>
> I have a bootstrap implementation loop that I would like to replace  
> by a faster "batch operator".
>
> The loop is as follows:
>
> for (b in 1:B) {
>  indices <- sample(1:N, size=N, replace=TRUE) # sample n elements  
> with replacement
>  theta_star[b,] = statistic(data,indices)     # execute statistic  
> callback function
> }
>
> I would like to replace this by something like (does not work):

Note: Loops may be just as fast or faster than apply calls.

>
> indices <- replicate(B,sample(1:N, size=N, replace=TRUE))
> theta_star <- apply(indices,2,statistic,data)

Why not:

theta_star <- apply(indices,2, function(x) statistic(data, x) )

May end up as a data class that needs further work, depending on what  
"statistic" return.

>
> This, however, does not work because the statistic function receives  
> indices as second parameter and not as first parameter. While I  
> could change the signature of my statistic function I would like to  
> know how to achieve this without having to do so.
>
> TIA,
> Best regards,
> Giovanni
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list