7

In R, the na.omit() function can be used to discard entries in a data.frame that contain NA values. As a side effect, if lines are indeed discarded, the function adds an attribute 'omit' to the result that contains a vector of the row.names that were discarded.

I want to discard this 'omit' attribute because I don't need it. What is the best way to do that?

joran
  • 169,992
  • 32
  • 429
  • 468
reddish
  • 1,360
  • 2
  • 12
  • 27
  • Or don't use na.omit. This isnt really what it's for anyway. – hadley Dec 08 '11 at 00:23
  • 2
    Hadley, your comment is confusing me. If na.omit is not really for discarding rows with NA from a data.frame, then what is it really for? – user1491868 Jun 09 '13 at 16:52
  • I'm also a little confused as to what @Hadley is recommending using here. I know there you could do purrr::discard(x, is.na) but not sure what the "correct" Base R function (as usually have used na.omit() or na.exclude() )... Anyone know what the right way to do this with Base R is though...? – Bryan Shalloway Mar 24 '23 at 04:36

1 Answers1

13

Just use data.frame after na.omit or you can do it directly:

> temp <- data.frame(a=c(1,NA,44),b=c(99,29,NA))
> new <- na.omit(temp)
> attributes(new)
$names
[1] "a" "b"

$row.names
[1] 1

$class
[1] "data.frame"

$na.action
2 3
2 3
attr(,"class")
[1] "omit"

> reduced <- data.frame(new)
> attributes(reduced)
$names
[1] "a" "b"

$row.names
[1] 1

$class
[1] "data.frame"
>

direct method:

attributes(new)$na.action <- NULL
Xu Wang
  • 10,199
  • 6
  • 44
  • 78