【问题标题】:Discarding a single attribute in R丢弃 R 中的单个属性
【发布时间】:2011-12-07 04:50:31
【问题描述】:

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

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

【问题讨论】:

  • 或者不要使用na.omit。无论如何,这并不是真正的目的。
  • 哈德利,你的评论让我很困惑。如果 na.omit 不是真正用于从 data.frame 中丢弃带有 NA 的行,那么它的真正用途是什么?

标签: r missing-data


【解决方案1】:

na.omit之后使用data.frame或者你可以直接使用:

> 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

【讨论】:

  • @joran 那绝对是第一次!
猜你喜欢
  • 1970-01-01
  • 2014-04-08
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
相关资源
最近更新 更多