Klausos Klausos
Klausos Klausos

Reputation: 16050

Select random elements from the list

How can I select random files (randomFiles) which would not contain f?

allFiles = list.files("D:/test")

for(f in allFiles)
{
   randomFiles = sample(allFiles, size = 10)
   #...
}

Upvotes: 1

Views: 639

Answers (1)

Vincent Guillemot
Vincent Guillemot

Reputation: 3429

This should work:

allFiles <- list.files("D:/test")

for( i in seq_along(allFiles) )
{
   randomFiles <- sample(allFiles[-i], size = 10)
   #...
}

Or you could also use one of those very useful set functions:

for( f in allFiles )
{
  randomFiles <- sample(setdiff(allFiles, f), size = 10)

  #...
}

Upvotes: 4

Related Questions