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
  • 707
  • 5
  • 14

2 Answers2

4

Use is.na

A[!is.na(B)]
#[1] 1 3 4
B[!is.na(B)]
#[1] 6 8 9
GKi
  • 20,626
  • 1
  • 11
  • 24
1

Something like

na.omit(cbind(A,B))
user2974951
  • 4,352
  • 1
  • 11
  • 18