1

I am trying to remove rows with NA in column 1 (row 4 in the example below), but instead of having the row removed, the entire row 4 is replaced with NA:

dat <- data.frame(x1 = c(1,2,3, NA, 5), x2 = c(100, NA, 300, 400, 500))
> dat
#  x1  x2
#  1  1 100
#  2  2  NA
#  3  3 300
#  4 NA 400
#  5  5 500

 ad<- dat[dat$x1!=1,]
 > ad
#     x1  x2
#  2   2  NA
#  3   3 300
#  NA NA  NA
#  5   5 500
M--
  • 25,431
  • 8
  • 61
  • 93
Wenwen
  • 13
  • 2

1 Answers1

0

try complete.cases:

ad <- dat[complete.cases(dat$x1), ]

which will only keep the rows in which dat$x1 does not contain NA

sweetmusicality
  • 937
  • 1
  • 10
  • 27