r - 如何在不丢失属性的情况下从data.frame删除行

标签 r statistics

对于初学者:到目前为止,我已经在这个问题上搜索了数小时-因此,如果答案不重要,请原谅我...

我想要做的是从data.frame中删除一行(第101号)。它包含测试数据,不应出现在我的分析中。我的问题是:每当我从data.frame中子集化时,属性(尤其是注释)都会丢失。

str(x)
# x has comments for each variable
x <- x[1:100,]
str(x)
# now x has lost all comments


有据可查的是,子集将删除所有属性-到目前为止,这很清楚。手册(例如http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.data.frame.html)甚至建议了一种保留属性的方法:

## keeping special attributes: use a class with a
## "as.data.frame" and "[" method:


as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d <- data.frame(i= 0:7, f= gl(2,4),
                u= structure(11:18, unit = "kg", class="avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"


我还不了解R,所以我才真正了解这里发生了什么。但是,仅运行这些行(最后三行除外)并不会更改我的子集的行为。使用带有适当矢量(100次TRUE + 1 FALSE)的子命令()会得到相同的结果。只是简单地将属性存储到变量中,然后在子集之后将其还原,也不起作用。

# Does not work...
tmp <- attributes(x)
x <- x[1:100,]
attributes(x) <- tmp


当然,我可以将所有注释写入向量(var => comment),子集并使用循环将它们写回-但这似乎不是一个有充分根据的解决方案。而且我很确定在以后的分析中会遇到具有其他相关属性的数据集。

因此,这就是我在stackoverflow,Google和脑力方面的努力陷入困境的地方。如果有人可以帮助我,我将不胜感激。谢谢!

最佳答案

如果我理解正确,那么您在data.frame中有一些数据,并且data.frame的列中有与之关联的注释。也许像下面这样?

set.seed(1)

mydf<-data.frame(aa=rpois(100,4),bb=sample(LETTERS[1:5],
  100,replace=TRUE))

comment(mydf$aa)<-"Don't drop me!"
comment(mydf$bb)<-"Me either!"


所以这会给你一些

> str(mydf)
'data.frame':   100 obs. of  2 variables:
 $ aa: atomic  3 3 4 7 2 7 7 5 5 1 ...
  ..- attr(*, "comment")= chr "Don't drop me!"
 $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2 2 5 4 2 1 3 5 3 ...
  ..- attr(*, "comment")= chr "Me either!"


而当您将此子集化时,将删除注释:

> str(mydf[1:2,]) # comment dropped.
'data.frame':   2 obs. of  2 variables:
 $ aa: num  3 3
 $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2


要保留注释,请像上面(从文档中一样)定义函数[.avector,然后将适当的类属性添加到data.frame中的每一列(编辑:以保持bb的因子水平,将"factor"添加到bb的类中。):

mydf$aa<-structure(mydf$aa, class="avector")
mydf$bb<-structure(mydf$bb, class=c("avector","factor"))


这样便保留了注释:

> str(mydf[1:2,])
'data.frame':   2 obs. of  2 variables:
 $ aa:Class 'avector'  atomic [1:2] 3 3
  .. ..- attr(*, "comment")= chr "Don't drop me!"
 $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2
  ..- attr(*, "comment")= chr "Me either!"


编辑:

如果data.frame中有许多要保留的属性列,则可以使用lapply(已编辑,以包括原始列类):

mydf2 <- data.frame( lapply( mydf, function(x) {
  structure( x, class = c("avector", class(x) ) )
} ) )


但是,这会删除与data.frame本身关联的注释(例如comment(mydf)<-"I'm a data.frame"),因此,如果有注释,请将其分配给新的data.frame:

comment(mydf2)<-comment(mydf)


然后你有

> str(mydf2[1:2,])
'data.frame':   2 obs. of  2 variables:
 $ aa:Classes 'avector', 'numeric'  atomic [1:2] 3 3
  .. ..- attr(*, "comment")= chr "Don't drop me!"
 $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2
  ..- attr(*, "comment")= chr "Me either!"
 - attr(*, "comment")= chr "I'm a data.frame"

关于r - 如何在不丢失属性的情况下从data.frame删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10404224/

相关文章:

r - 在R的申请中纳入外部功能

R data.table : how to go from tibble to data. 表返回?

java - 从 JAVA 调用 R 以获得卡方统计量和 p 值

python - 如何在 python 中插入地理引用数据?

java - 如何在weka中使用不同的缩放方法

r - 地理编码失败,状态为 REQUEST_DENIED,位置 = "Australia"在 R 中使用 ggmap

r - 使用gsub替换字符,如何创建函数?

r - 使用 ggplot2 在给定的 x 值处绘制数据帧的每行一行

PHP 网站统计

sql - T-SQL 中的幂律分布