Search code examples
rvectordataframeindices

Remove same indices from two vectors


I have two vectors in R, e.g.

a <- c(2,6,4,9,8)
b <- c(8,9,4,2,1)

Vectors a and b are ordered in a way that I wish to conserve (I will be plotting them against each other). I want to remove certain values from vector a and remove the values at the same indices in b. e.g. if I wanted to remove values ≥ 8 from a:

a <- a[a<8]

... which gives a new vector without those values.

Is there now an easy way of removing values from the same indices in b (in this example indices 4 and 5)? Perhaps by using a data frame?


Solution

  • Something like this:

    keep <- a < 8
    a <- a[keep]
    b <- b[keep]
    

    You could also use:

    keep <- which( a < 8 )