R - 简单循环不起作用:break/next 没有循环,跳转到顶层

R - Simple loop not working: no loop for break/next, jumping to top level

我知道这可能是一个简单的问题,但我正在努力学习和改进。当我尝试这段代码时,它给了我错误:“break/next 没有循环,跳转到顶层”。有人可以建议为什么并帮助我吗?真的非常感谢。

x_1 <- rnorm(100)
x_2 <- rnorm(10000)
x_3 <- rnorm(1000000)

to_evaluate <- list(x_1, x_2, x_3)

speed_test <- for (i in to_evaluate) {
  microbenchmark(mean_loop(i), mean_mat(i), mean(i))
} 
print(speed_test)

mean_loop 和 mean_mat 的代码:

x <- c(1:11)

mean_loop <- function(x) {
  
  sum_of_x <- 0
  
  for(i in x){
    sum_of_x <- sum_of_x + x[i]
  }
  
  mean_of_x <- sum_of_x/length(x)
  return(mean_of_x)
}

mean_mat <- function(x) {
sum(diag(length(x))%*%x)/length(x)
}

函数 microbenchmark(来自包 microbenchmark)可让您测量代码 运行 的速度。如果你给它代码来评估,它会评估它 100 次,并且 return 汇总统计代码用了多长时间 运行。如果你给它多个表达式,它会为每个表达式执行此操作。

使用lapply()你可以:

speed_test <- lapply(to_evaluate, function(x) microbenchmark(mean_loop(x), mean_mat(x), mean(x)))
print(speed_test)

示例:

# had to remove x_3 to overcome a crash not linked with the use of a loop or lapply, see the Note at the end of the post
x_1 <- rnorm(100)
x_2 <- rnorm(10000)
to_evaluate <- list(x_1, x_2)

library(microbenchmark)

speed_test <- lapply(to_evaluate, function(x) microbenchmark(mean_loop(x), mean_mat(x), mean(x)))

输出:

> print(speed_test)

[[1]]
Unit: microseconds
         expr  min    lq   mean median   uq    max neval cld
 mean_loop(x) 35.3 36.05 67.174  36.85 37.3 3000.9   100   a
  mean_mat(x) 19.7 20.25 38.090  20.60 20.9 1761.0   100   a
      mean(x)  2.0  2.30  2.793   2.70  2.9   13.8   100   a

[[2]]
Unit: microseconds
         expr      min        lq       mean    median        uq      max neval cld
 mean_loop(x) 126214.8 139520.40 211537.906 182460.50 245618.95 463139.9   100  b 
  mean_mat(x) 275451.1 314082.60 337446.084 340836.75 358470.35 411152.0   100   c
      mean(x)     16.7     20.15     31.192     33.35     35.35     82.0   100 a  

注:

是否在我的计算机上使用 lapply 的循环,我遇到了与您的问题无关的性能问题。 即使这对我不起作用 microbenchmark(mean_loop(to_evaluate[[3]]), mean_mat(to_evaluate[[3]]), mean(to_evaluate[[3]])) 而其他 运行 很好(to_evaluate[[1]]to_evaluate[[2]])。

这对我有用,使用问题中的函数定义 -

x_1 <- rnorm(10)
x_2 <- rnorm(100)
x_3 <- rnorm(1000)
library(microbenchmark)
to_evaluate <- list(x_1, x_2, x_3)

result <- vector('list', length(to_evaluate))

for (i in seq_along(to_evaluate)) {
  val <- to_evaluate[[i]]
  result[[i]] <- microbenchmark(mean_loop(val), mean_mat(val), mean(val))
} 

result

#[[1]]
#Unit: microseconds
#           expr   min     lq    mean median    uq     max neval cld
# mean_loop(val) 2.818 3.2495 7.62632  4.204 5.847  41.214   100   a
#  mean_mat(val) 2.547 2.9765 7.90376  3.571 4.940  93.155   100   a
#      mean(val) 1.896 2.1700 6.59605  2.818 4.204 116.467   100   a

#[[2]]
#Unit: microseconds
#           expr    min      lq     mean median      uq     max neval cld
 #mean_loop(val) 49.368 89.8705 92.81099 92.820 99.6505 179.400   100   c
 # mean_mat(val) 18.968 50.7335 55.61163 52.987 55.5770 141.363   100  b 
 #     mean(val)  2.090  2.2815  3.87982  2.803  3.3115  33.779   100 a  

#[[3]]
#Unit: microseconds
#           expr      min        lq       mean   median        uq       max neval cld
# mean_loop(val) 1359.527 1511.9295 3312.36578 1835.326 2970.1350 16455.048   100   b
#  mean_mat(val) 1713.178 2155.3815 4032.53143 2352.858 2739.6650 19783.264   100   b
#      mean(val)    3.708    4.6695   16.39451   14.881   21.8355   162.184   100  a