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



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

您可以考虑创建一个全局计数器,并指定何时打印进度,例如,您可以在处理 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"

您可以像这样将 print 语句添加到您正在使用的函数中

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

不漂亮,但做你想做的事

最新更新