7

在 R 中,该na.omit()函数可用于丢弃 data.frame 中包含 NA 值的条目。作为副作用,如果确实丢弃了行,则该函数将属性“省略”添加到包含被丢弃的 row.names 的向量的结果中。

我想丢弃这个“省略”属性,因为我不需要它。最好的方法是什么?

4

1 回答 1

13

只需使用data.frameafterna.omit或者您可以直接使用:

> 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"
>

直接法:

attributes(new)$na.action <- NULL
于 2011-12-07T04:58:02.753 回答