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
  • 909
  • 7
  • 22

2 Answers2

4

Use is.na

A[!is.na(B)]
#[1] 1 3 4
B[!is.na(B)]
#[1] 6 8 9
GKi
  • 37,245
  • 2
  • 26
  • 48
1

Something like

na.omit(cbind(A,B))
user2974951
  • 9,535
  • 1
  • 17
  • 24