在子集化时保留R对象的class属性

时间:2021-04-19 20:33:02

I have an R object called gene_table, and it has class foo. Now I subset this gene_table by

我有一个名为gene_table的R对象,它有类foo。现在我将这个gene_table归类为

gene_data = gene_table[1:100,1:5]

gene_data = gene_table [1:100,1:5]

However, when I call class(gene_data), it is no longer of class foo, but instead, it has class matrix. This is a headache for me because my method summary.foo won't recognize this object gene_data of class matrix. I am hoping to retain the original class attribute when subsetting, so could anyone tell me how to do it? Thanks!

但是,当我调用class(gene_data)时,它不再是类foo,而是具有类矩阵。这对我来说很头疼,因为我的方法summary.foo不会识别类矩阵的这个对象gene_data。我希望在子集化时保留原始的class属性,所以有人能告诉我怎么做吗?谢谢!

Update: dput(head(gene_table)) gives me

更新:dput(head(gene_table))给了我

c(5.21708054951994, 5.01224214039806, 4.92160314073853, 4.83031021496, 4.78552614584879, 4.77821370665578)

and str(gene_table) gives me

和str(gene_table)给了我

 foo [1:22743, 1:2] 5.22 5.01 4.92 4.83 4.79 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:22743] "ENSG00000127954" "ENSG00000151503" "ENSG00000096060" "ENSG00000091879" ...
  ..$ : chr [1:2] "Var1" "Var2"

1 个解决方案

#1


5  

You could use something like this as your definition for [.foo:

你可以使用这样的东西作为[.foo的定义:

`[.foo` <- function(x, ..., drop=TRUE) {
   structure(NextMethod(), class="foo")
}

You may need to add other things, depending on the complexity of your "foo" class.

您可能需要添加其他内容,具体取决于“foo”类的复杂程度。

#1


5  

You could use something like this as your definition for [.foo:

你可以使用这样的东西作为[.foo的定义:

`[.foo` <- function(x, ..., drop=TRUE) {
   structure(NextMethod(), class="foo")
}

You may need to add other things, depending on the complexity of your "foo" class.

您可能需要添加其他内容,具体取决于“foo”类的复杂程度。