R:将term.formula应用于继承data.frame的S4对象 [英] R: Applying terms.formula on an S4 object inheriting data.frame

查看:104
本文介绍了R:将term.formula应用于继承data.frame的S4对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个继承自数据框架的新类:

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.

如果我对这里的问题是正确的,无论如何我都可以制定条件.公式可以识别出我正在给它一个data.frame的事实吗?

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行上失败:

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

问题必须是对.Internal(terms.formula())的调用期望使用'plain old'data.frame,而是将其传递给类new.frame的对象. 作为一种解决方法,为什么不只是传递terms.formula()它期望的对象类型(未经修饰的data.frame)呢?

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)?

这是一种简单的方法:

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>

这篇关于R:将term.formula应用于继承data.frame的S4对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆