0

I'm quite new to R, but I'm trying to make a "randomizer" of sorts.

I have a vector

  • names <- c('Name1', 'Name2', [...], 'Name13')

I then sample 6 names from the vector to another vector

  • name_sample_1 <- sample(names, 6)

What i want is to then update the "names" vector by a line of code, and not have to do it manually. I tried running:

  • names <- names - name_sample_1

But this returned the error 'non-numeric argument to binary operator'. Any ideas on how to do this effectively?

  • Can you include the exact output vector you want here? – Tim Biegeleisen Feb 11 '21 at 08:57
  • Ah, sorry! What I want output is to update the names vector so that it does not contain the names of the sampled vector. The comment below solved the issue for me, but thank you for you interest – Cristoffer Sakshaug Feb 11 '21 at 09:33
  • You can also use `setdiff`. Also It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Feb 11 '21 at 09:39

1 Answers1

0

you have to use the handy %in% operator!

names <- paste0("name", 1:20)
sample_names <- sample(names,6)

names_updated <- names[!names %in% sample_names]
tlhenvironment
  • 319
  • 2
  • 10