R:在继承data.frame的S4对象上应用terms.formula(R: Applying terms.formula on an S4 object inheriting data.frame

编程入门 行业动态 更新时间:2024-10-08 18:33:40
R:在继承data.frame的S4对象上应用terms.formula(R: Applying terms.formula on an S4 object inheriting data.frame)

我试图创建一个从数据框继承的新类:

> setClass('new.frame', representation(colour='character'), contains = 'data.frame')

这是该类的一个实例,用于测试:

> test_data = data.frame(cbind(runif(5), runif(5))) > names(test_data) = c('X', 'Y') > test_frame = new('new.frame', test_data, colour='red')

只是为了确保它看起来没问题...

> data.frame Object of class "new.frame" X Y 1 0.8766306 0.4741213 2 0.1221508 0.5117665 3 0.4838761 0.4973627 4 0.7858294 0.4064749 5 0.5147703 0.9135304 Slot "colour": [1] "red"

...并确保继承有效

> is.data.frame(test_frame) [1] TRUE > getClass(class(test_frame)) Class "new.frame" [in ".GlobalEnv"] Slots: Name: .Data colour names Class: list character character Name: row.names .S3Class Class: data.frameRowLabels character Extends: Class "data.frame", directly Class "list", by class "data.frame", distance 2 Class "oldClass", by class "data.frame", distance 2 Class "vector", by class "data.frame", distance 3

以下是我尝试利用数据框的特性时遇到的问题:

> terms.formula(Y ~ X, data = test_frame) Error in terms.formula(Y ~ X, data = test_frame) : 'data' argument is of the wrong type

我可能错过了一些愚蠢的东西。 如果是这样,提前谢谢指出。

如果我对这里的问题是正确的,那么无论如何,我可以做出terms.formula承认事实,我给它一个data.frame?

I'm trying to create a new class that inherits from data frame:

> setClass('new.frame', representation(colour='character'), contains = 'data.frame')

This is an instance of that class, for testing:

> test_data = data.frame(cbind(runif(5), runif(5))) > names(test_data) = c('X', 'Y') > test_frame = new('new.frame', test_data, colour='red')

Just to make sure it looks all right...

> data.frame Object of class "new.frame" X Y 1 0.8766306 0.4741213 2 0.1221508 0.5117665 3 0.4838761 0.4973627 4 0.7858294 0.4064749 5 0.5147703 0.9135304 Slot "colour": [1] "red"

... and to make sure that the inheritance worked

> is.data.frame(test_frame) [1] TRUE > getClass(class(test_frame)) Class "new.frame" [in ".GlobalEnv"] Slots: Name: .Data colour names Class: list character character Name: row.names .S3Class Class: data.frameRowLabels character Extends: Class "data.frame", directly Class "list", by class "data.frame", distance 2 Class "oldClass", by class "data.frame", distance 2 Class "vector", by class "data.frame", distance 3

Here is the problem I encountered when I tried to utilize the property of being a data frame:

> terms.formula(Y ~ X, data = test_frame) Error in terms.formula(Y ~ X, data = test_frame) : 'data' argument is of the wrong type

I might have missed something silly. If so, thanks in advance for pointing it out.

If I'm correct about the problem here, is there anyway I can make terms.formula recognize the fact that I'm giving it a data.frame?

最满意答案

执行debug(terms.formula)然后运行terms.formula(Y ~ X, data = test_frame)显示代码在引用代码块的第3行和第4行失败:

if (!is.null(data) && !is.environment(data) && !is.data.frame(data)) data <- as.data.frame(data, optional = TRUE) terms <- .Internal(terms.formula(x, specials, data, keep.order, allowDotAsName))

问题一定是对.Internal(terms.formula())的调用需要一个“普通旧式” data.frame ,而不是传递一个new.frame类的对象。 作为一种解决方法,为什么不通过terms.formula()它所期望的对象类型(一个未经修改的data.frame)?

这是一个简单的方法来做到这一点:

terms.formula(Y ~ X, data = unclass(test_frame)) # Y ~ X # attr(,"variables") # list(Y, X) # attr(,"factors") # X # Y 0 # X 1 # attr(,"term.labels") # [1] "X" # attr(,"order") # [1] 1 # attr(,"intercept") # [1] 1 # attr(,"response") # [1] 1 # attr(,".Environment") # <environment: R_GlobalEnv>

Doing debug(terms.formula) and then running terms.formula(Y ~ X, data = test_frame) shows that your code is failing on lines 3 and 4 of the quoted code block:

if (!is.null(data) && !is.environment(data) && !is.data.frame(data)) data <- as.data.frame(data, optional = TRUE) terms <- .Internal(terms.formula(x, specials, data, keep.order, allowDotAsName))

The problem must be that the call to .Internal(terms.formula()) expects a 'plain old' data.frame, and is instead being passed an object of class new.frame. As a workaround, why not just pass terms.formula() the type of object it expects (an unadorned data.frame)?

Here's one easy way to do that:

terms.formula(Y ~ X, data = unclass(test_frame)) # Y ~ X # attr(,"variables") # list(Y, X) # attr(,"factors") # X # Y 0 # X 1 # attr(,"term.labels") # [1] "X" # attr(,"order") # [1] 1 # attr(,"intercept") # [1] 1 # attr(,"response") # [1] 1 # attr(,".Environment") # <environment: R_GlobalEnv>

更多推荐

本文发布于:2023-08-04 19:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   data   frame   terms   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!