0

Currently I have data that looks like this (just with significantly more NA filled columns and significantly more rows that are exclusively NAs):

Column 1 Column 2 Column 3
NA NA NA
Texas Oklahoma NA
NA Florida Florida
NA NA NA

I'd like for it to look like this

Column 1 Column 2 Column 3
Texas Oklahoma NA
NA Florida Florida

I don't want to get rid of all rows that have an NA value, I just want to get rid of all rows that have nothing but NA values.

Thanks in advance.

2 Answers2

1

You can use janitor package:

janitor::remove_empty(dat, which = "rows")
Bloxx
  • 1,495
  • 1
  • 9
  • 21
0

If you want to get rid of rows that contain only NA but keep rows that have some NA values you could combine is.na and rowSums

dat <- dat[rowSums(!is.na(dat)) > 0, ]

is.na will return a TRUE or FALSE which will be treated as 1 and 0 for the rowSums. Any row with a value that is not NA will have a sum greater than 0.

Tjn25
  • 685
  • 5
  • 18