0

Let say I have below data frame

set.seed(100)
DF = data.frame(col1 = 1:10, col2 = sample(letters[1:10], 10))

Let define a vector

vec = c("c", "a", "b")

Now I want to split above DF based on the values of the col2. Means, in the first part I will have all values of col2 that are in vec, and in the second part the remaining part.

Bogaso
  • 2,838
  • 3
  • 24
  • 54

1 Answers1

0

if you want to have in the first rows the values that are in vec, you can create two data frames, one that corresponds to the values in vec and one where they're not.

Then, you can concatenate them with rbind:

in.vec = DF$col2 %in% vec
new.DF = rbind(DF[in.vec,], DF[!in.vec]) 

where DF[in.vec,] selected the rows for which the values of col2 can be found in vec.

glagla
  • 611
  • 4
  • 9