-1

How can I remove a NA value in R and at the same time, the row above and below the NA value? The NA's are in 2 different columns in a large dataset

So in total removing 3 rows at the time for each NA.

Center_x Center_y
200.3    400
NA       200.2
300      100
400.1    450
200      100
100      NA
200      100

I want to get

Center_x Center_y
400.1    450

Naomi
  • 1
  • 1
  • 2
    Welcome to Stack Overflow. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(yourdata)`, if that is not too large. – neilfws Dec 06 '21 at 21:38

1 Answers1

0

Assuming there's only 1 NA en the complete column1:

na_row <- is.na(df$column1)

na_row_number <- which(is.na(na_row))

remove_rows <- c((na_row_number-1):(na_row_number+1))

df <- df[-remove_rows,]

I think this should work