0

I was wondering if it was possible to do something like this using a loop:

data[data!=1]
data[data!=1&data!=2]
data[data!=1&data!=2&data!=3]
ThatsNotMyName
  • 512
  • 2
  • 7
  • 23

1 Answers1

1

Good afternoon :)

You could try:

data <- c(1,2,3,2,2,1,4,5,2,1)
indices <- 1:3 # or use function seq for more general sequences
for(i in indices){
  print(data[!data %in% min(indices):i])
}

If you want to assign back to new vectors, you can try lapply:

obj_list <- lapply(indices, FUN = function(i){
  data[!data %in% min(indices):i]
})
obj_list

Is this what you are looking for?

Jonny Phelps
  • 2,329
  • 9
  • 17
  • Thanks, that set me on track for what I needed to do. – ThatsNotMyName Sep 18 '17 at 16:28
  • Hi, quick follow up question, if I was trying to subset by strings instead, how could I do that? Thanks – ThatsNotMyName Sep 18 '17 at 16:57
  • Do you have an example? an alternate way of doing what I've done is with the `subset` function. if you have strings like `string_vec = c("a", "b", "c")`. Then you can do `data[!data %in% string_vec[min(indices):i] ]`. That is, define a numeric vector from 1 to length of your strings. Then do the same thing but do `string_vec[num_vec]`. Make sense? – Jonny Phelps Sep 18 '17 at 17:13
  • I was trying to follow the the answer shown in https://stackoverflow.com/questions/26612805/r-histogram-with-multiple-populations but with a larger data set. – ThatsNotMyName Sep 18 '17 at 17:19