可以通过使用R函数(如 apply
)(或其亲属, sapply
和 (b)
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ :N,function(i){x < - rnorm(2); return(x [2] -x [1])})
}
> system.time(res3 <-fn3(N))
用户系统流逝的
0.397 0.004 0.397
Suppose I want perform a simulation using the following function
:
fn1 <- function(N) {
res <- c()
for (i in 1:N) {
x <- rnorm(2)
res <- c(res, x[2]-x[1])
}
res
}
For very large N
, computation appears to hang. Are there better ways of doing this?
(Inspired by: https://stat.ethz.ch/pipermail/r-help/2008-February/155591.html)
解决方案
For loops in R are notoriously slow, but here there's another issue. It's much faster to preallocate the results vector, res, rather append to res at each iteration.
Below we can compare the speed of the above version with a version that simply starts with a vector, res, of length N and changes the ith element during the loop.
fn1 <- function(N) {
res <- c()
for (i in 1:N) {
x <- rnorm(2)
res <- c(res,x[2]-x[1])
}
res
}
fn2 <- function(N) {
res <- rep(0,N)
for (i in 1:N) {
x <- rnorm(2)
res[i] <- x[2]-x[1]
}
res
}
> N <- 50000
> system.time(res1 <- fn1(N))
user system elapsed
6.568 0.256 6.826
> system.time(res2 <- fn2(N))
user system elapsed
0.452 0.004 0.496
Also, as Sharpie points out, we can make this slightly faster by using R functions like apply
(or its relatives, sapply
and lapply
).
fn3 <- function(N) {
sapply( 1:N, function( i ){ x <- rnorm(2); return( x[2] - x[1] ) } )
}
> system.time(res3 <- fn3(N))
user system elapsed
0.397 0.004 0.397
这篇关于大循环挂在R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!