1

I have two vectors, say A and B

A <- c(1, 2, 3, 4, 5)
B <- c(6, NA, 8, 9, NA)

I would like to exclude elements in A corresponding to the elements of B which comprise NAs.

So, I am in need of an automatic way to remove indices 2 and 5 from both A and B, so that the length of both vectors is the same.

compbiostats
  • 811
  • 7
  • 17

2 Answers2

4

Use is.na

A[!is.na(B)]
#[1] 1 3 4
B[!is.na(B)]
#[1] 6 8 9
GKi
  • 27,870
  • 2
  • 18
  • 35
1

Something like

na.omit(cbind(A,B))
user2974951
  • 7,144
  • 1
  • 13
  • 21