0

I have a big vactor (3GB) to process thru lapply, and I would like to know how much it has proceeded. It is easier if I can identify which element it is, but what if I can't?

In the for loop, I may assign a counter inside the loop, but how should I do to set a counter inside lapply?

X <- LETTERS[1:26]

lapply(X, function(a) {
  cat(paste0(which(X == a), "\r")
  print(a)
})

Many thanks.

Grec001
  • 1,111
  • 6
  • 20
  • 1
    `lapply` is little more than syntactic sugar over a for loop (although that [oversimplifies](https://stackoverflow.com/q/2275896/4996248) things). Why not just use a loop -- or (with 3GB) find a way to genuinely vectorize whatever it is that you are doing? – John Coleman Nov 03 '19 at 13:22

2 Answers2

1

To roll your own progress counter just iterate over X's integer positions instead of iterating over X's elements directly. The following prints the percentage processed:

X_out <- lapply(seq_along(X), function(a) {
    cat("\r", round((a/length(X))*100), "%", sep = "")
    toupper(X[a])
})

You can also try the progress package. The only downside is that it seems to slow things down a bit (perhaps by design, so you can see the progress). You could try something like this:

install.packages("progress")
library(progress)

X <- sample(letters, 100000, T)

pb <- progress_bar$new(total = length(X))

X_out <- lapply(X, function(a) {
    pb$tick()
    toupper(a)
})
0

Iterate over the indexes rather than the vector itself:

X <- LETTERS
f <- function(i) {
  if (i %% 10 == 0) cat("X[", i, "] =", X[i], "\n")
  X[i]
}
result <- lapply(seq_along(X), f)

giving this output on the R console (as well as setting result):

X[ 10 ] = J 
X[ 20 ] = T 

Alternately iterate over both the index and the vector:

f2 <- function(i, x) {
  if (i %% 10 == 0) cat("X[", i, "] =", x, "\n")
  x
}
result2 <- Map(f2, seq_along(X), X)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341