当 运行 R 中的应用函数时,有没有办法打印迭代次数

Is there a way to print the number of iteration when running an apply function in R

我 运行 一个大数据集上的 apply-family 函数,所以我想知道是否有办法知道到目前为止工作进展如何,到目前为止查看了多少元素,或者类似的东西这个?

您可以像这样向正在使用的函数添加打印语句

apply(mtcars,2, function(i) {print(i[1])
mean(i)})

不漂亮但做你想做的事

您可以考虑创建一个全局计数器,并指定何时打印进度,例如您可以在处理完10%的数据时打印通知;

counter <- 0
data <- rnorm(100)
results <- sapply(data, function(x) { 
                  counter <<- counter + 1; 
                  if(counter %in% seq(0, length(y), 10)) 
                      print(paste(counter, "% has been processed"))})

[1] "10 % has been processed"
[1] "20 % has been processed"
[1] "30 % has been processed"
[1] "40 % has been processed"
[1] "50 % has been processed"
[1] "60 % has been processed"
[1] "70 % has been processed"
[1] "80 % has been processed"
[1] "90 % has been processed"
[1] "100 % has been processed"