Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iteration

r

apply

I run an apply-family function over a big dataset and so I wonder if there is a way to know how is going the job so far, how many elements got viewed so far, or something like this?

like image 218
hans glick Avatar asked Jun 03 '16 19:06

hans glick


2 Answers

You can consider creating a global counter, and specify when you want to print the progress, for example, you can print a notice whenever 10% of your data has been processed;

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"
like image 196
Psidom Avatar answered Oct 27 '22 00:10

Psidom


You could add a print statement to the function you are using like this

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

Isn't pretty but does what you want

like image 28
Carl Avatar answered Oct 27 '22 00:10

Carl