0

I wrote a custom function to divide a data frame into k folds for k-fold cross-validation and want to print out the start and end indices of the folds to check if the division was done correctly. I'm using the paste() function within a for loop. When I run the function, I get no error but nothing gets printed. When I copy just the paste() line into the terminal it prints the last index correctly, which suggests that the function was executed correctly. So why it is not printing the paste() line from within the function? I would be grateful for any advice on this.

fold_index_counter = function(k, data){

  n = nrow(data)

  for (i in 1:k){
    start = (n*i)/k
    end = (n*(i+1))/k-1
    paste("Fold no. ", i, " starts at row ", start, " and ends at row ", end, sep = "")
  }  
}

fold_index_counter(10, dat.cor.trim)
Des Grieux
  • 520
  • 1
  • 5
  • 31
  • 2
    Within a loop use a `print()`, `cat()` or even a `message()` function. Edit: there is a bigger problem, you want to print inside a function, which as well demans a "printing" function. – m-dz Mar 08 '17 at 20:26
  • And if you want to print something immediately without waiting for the whole loop to end (as in your situation) `cat()` and `flush.console()` might be needed. – m-dz Mar 08 '17 at 20:34
  • Thank you! This is very helpful. – Des Grieux Mar 09 '17 at 00:51

0 Answers0