wake_wake
wake_wake

Reputation: 1204

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.

Upvotes: 0

Views: 152

Answers (1)

Batanichek
Batanichek

Reputation: 7871

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)
}

Upvotes: 2

Related Questions