How to increase the size of an iterator in a for-loop in R?

I have a very simple for loop that iterates over each of the 100 elements of a character vector called doc.

Maybe something like:

for (i in seq_along(doc)){
  ytime <- proc.time()
  mycorpus<- VCorpus(VectorSource(doc[i]))
  ... some other functions ...
  print(proc.time() - ytime)
}

Instead of iterating over each single element, is it possible to let i be chunks of (say) ten elements of doc?

Such that it only takes 10 iterations to sequence along doc and ten elements of doc are fed to VCorpus at once.

Answer

You can hard code it like

n=10
for (i in seq_len(length(doc)/n)){
ytime <- proc.time()
  mycorpus<- VCorpus(VectorSource(doc[(n*(i-1)+1):(n*i)]))
  ... some other functions ...
  print(proc.time() - ytime)
}

Leave a Reply

Your email address will not be published. Required fields are marked *