R:访问字段值
我想知道如何访问 R 对象中包含的各个字段。或者,更准确地说,如何让 R 告诉我如何做。
例如,如果我运行以下代码:
dx.ct <- ur.df(dat1[,'dx'], lags=3, type='trend')
summary(dx.ct)
那么我会得到以下输出:
###############################################
# Augmented Dickey-Fuller Test Unit Root Test #
###############################################
Test regression trend
Call:
lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
Residuals:
Min 1Q Median 3Q Max
-0.46876 -0.24506 0.02420 0.15752 0.66688
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.099231 0.561377 1.958 0.0606 .
z.lag.1 -0.239438 0.141093 -1.697 0.1012
tt -0.019831 0.007799 -2.543 0.0170 *
z.diff.lag1 -0.306326 0.193001 -1.587 0.1241
z.diff.lag2 -0.214229 0.186135 -1.151 0.2599
z.diff.lag3 -0.223433 0.179040 -1.248 0.2228
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3131 on 27 degrees of freedom
Multiple R-squared: 0.3326, Adjusted R-squared: 0.209
F-statistic: 2.691 on 5 and 27 DF, p-value: 0.04244
Value of test-statistic is: -1.697 2.4118 3.2358
Critical values for test statistics:
1pct 5pct 10pct
tau3 -4.15 -3.50 -3.18
phi2 7.02 5.13 4.31
phi3 9.31 6.73 5.61
因此,我知道我应该能够单独访问上面的所有值,但我不知道如何指向它们。有什么方法可以让 R 告诉我它们是如何存储的吗?
我正在思考:
showobjects(summary(dx.ct))
然后它输出
$formula
$residuals
$coefficients
etc.
,然后我可以做
showobjects(summary(dx.ct)$residuals)
然后输出
$min
$1Q
$median
etc.
谢谢
卡尔
I would like to know how I can access the individual fields contained in an R object. Or, more precisely, how to get R to tell me how.
For example, if I run the following code:
dx.ct <- ur.df(dat1[,'dx'], lags=3, type='trend')
summary(dx.ct)
then I get this output:
###############################################
# Augmented Dickey-Fuller Test Unit Root Test #
###############################################
Test regression trend
Call:
lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
Residuals:
Min 1Q Median 3Q Max
-0.46876 -0.24506 0.02420 0.15752 0.66688
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.099231 0.561377 1.958 0.0606 .
z.lag.1 -0.239438 0.141093 -1.697 0.1012
tt -0.019831 0.007799 -2.543 0.0170 *
z.diff.lag1 -0.306326 0.193001 -1.587 0.1241
z.diff.lag2 -0.214229 0.186135 -1.151 0.2599
z.diff.lag3 -0.223433 0.179040 -1.248 0.2228
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3131 on 27 degrees of freedom
Multiple R-squared: 0.3326, Adjusted R-squared: 0.209
F-statistic: 2.691 on 5 and 27 DF, p-value: 0.04244
Value of test-statistic is: -1.697 2.4118 3.2358
Critical values for test statistics:
1pct 5pct 10pct
tau3 -4.15 -3.50 -3.18
phi2 7.02 5.13 4.31
phi3 9.31 6.73 5.61
So, I know that I should be able to access all of the values above individually, I don't know how to point to them. Is there some way to ask R to show me how they are stored?
I am thinking along the lines of:
showobjects(summary(dx.ct))
And then it outputs
$formula
$residuals
$coefficients
etc.
and then I can do
showobjects(summary(dx.ct)$residuals)
which then outputs
$min
$1Q
$median
etc.
Thanks
Karl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
作为另一种选择,请查看
属性
。例如,如果您不需要完整的输出,则
names(attributes(lc.df))
仅返回嗯,名称。As another alternative, take a look at
attributes
. e.g.If you don't want the full output, then
names(attributes(lc.df))
returns only the, um, names.除了
unclass()
之外,还可以尝试str()
。或者阅读源代码,看看其他访问器是如何做到的。编辑:例如,这里是您正在查看的
ur.df
对象的 S4 类方法summary
的源代码:它使用S4 对象元素的标准
@
访问器。Besides
unclass()
, trystr()
. Or read the source code to see how the other accessors do it.Edit: Here is for example the source code of the S4 class method
summary
for theur.df
object you were looking at:and it uses a standard
@
accessor for S4 object elements.您可以做的一件事是使用
unclass()
。使用名称来调查它:
然后引用特定值:
One thing that you can do is use
unclass()
.Use names to investigate it:
And then reference a specific value:
正如 Dirk Eddelbuettel 提到的,您可以通过 @ 获取此 S4 类型类的指定字段值。
You can get the specified field value via @ for this S4 type class as Dirk Eddelbuettel mentioned.