gpt4 book ai didi

r - 带有 S4 对象槽的 tbl_df

转载 作者:行者123 更新时间:2023-12-01 19:33:15 25 4
gpt4 key购买 nike

第一次尝试在 R 中进行 OO 编程时,我声明了一个 S4 对象 TestObject,它有两个插槽,其中一个插槽 (test.df) 具有允许的 data.frame 类:

library("dplyr")
library("methods")

# Declare S4 Object
setClass("TestObject",
slots = list(test.mat = "matrix",
test.df = "data.frame"))

令我惊讶的是,当我尝试分配 tbl_df“类”的对象时:

library("nycflights13")
class(flights)
## [1] "tbl_df" "tbl" "data.frame"

它抛出一个错误:

test.object <- new("TestObject")
slot(test.object, "test.mat") <- matrix(rnorm(10), 5, 2)
slot(test.object, "test.df") <- flights
## Error in checkSlotAssignment(object, name, value): c("assignment of an object of class \"tbl_df\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE", "assignment of an object of class \"tbl\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE", "assignment of an object of class \"data.frame\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE")

我的印象是 tbl_df 只是围绕着一个 data.frame。我想也许我可以将 tbl_df 声明为 test.df 的允许类:

setClass("TestObject",
slots = list(test.mat = "matrix",
test.df = "tbl_df"))
## Warning: undefined slot classes in definition of "TestObject":
## test.df(class "tbl_df")

但它似乎没有将其识别为允许的类。

看来我必须将 tbl_df 转换成 data.frame,才能完成这项工作......

setClass("TestObject",
slots = list(test.mat = "matrix",
test.df = "data.frame"))

slot(test.object, "test.df") <- as.data.frame(flights)
## [1] "data.frame"

但是这样做会失去将它作为 tbl_df 的好处(例如打印几行,所有列都适合一个屏幕等)

class(slot(test.object, "test.df"))
head(slot(test.object, "test.df"))
## year month day dep_time dep_delay arr_time arr_delay carrier tailnum
## 1 2013 1 1 517 2 830 11 UA N14228
## 2 2013 1 1 533 4 850 20 UA N24211
## 3 2013 1 1 542 2 923 33 AA N619AA
## 4 2013 1 1 544 -1 1004 -18 B6 N804JB
## 5 2013 1 1 554 -6 812 -25 DL N668DN
## 6 2013 1 1 554 -4 740 12 UA N39463
## flight origin dest air_time distance hour minute
## 1 1545 EWR IAH 227 1400 5 17
## 2 1714 LGA IAH 227 1416 5 33
## 3 1141 JFK MIA 160 1089 5 42
## 4 725 JFK BQN 183 1576 5 44
## 5 461 LGA ATL 116 762 5 54
## 6 1696 EWR ORD 150 719 5 54

我有一种感觉,我不理解 tbl_df(也许它不是一个类)。但是,有没有办法让 tbl_df“类”与 S4 和 data.frame 允许的类一起工作?

完整代码可以在这里找到:

https://github.com/tinyheero/tbl-df-s4/blob/master/tbl-df-s4.Rmd

和相应的html:

https://htmlpreview.github.io/?https://github.com/tinyheero/tbl-df-s4/blob/master/tbl-df-s4.html

R session 详细信息

devtools::session_info()
## Session info --------------------------------------------------------------
## setting value
## version R version 3.2.2 (2015-08-14)
## system x86_64, darwin14.5.0
## ui X11
## language (EN)
## collate en_CA.UTF-8
## tz America/Vancouver
## date 2016-02-25
## Packages ------------------------------------------------------------------
## package * version date source
## assertthat 0.1 2013-12-06 CRAN (R 3.2.2)
## DBI 0.3.1 2014-09-24 CRAN (R 3.2.2)
## devtools 1.10.0 2016-01-23 CRAN (R 3.2.2)
## digest 0.6.9 2016-01-08 CRAN (R 3.2.2)
## dplyr * 0.4.3 2015-09-01 CRAN (R 3.2.2)
## evaluate 0.8 2015-09-18 CRAN (R 3.2.2)
## formatR 1.2.1 2015-09-18 CRAN (R 3.2.2)
## htmltools 0.3 2015-12-29 CRAN (R 3.2.2)
## knitr * 1.12.3 2016-01-22 CRAN (R 3.2.2)
## magrittr 1.5 2014-11-22 CRAN (R 3.2.2)
## memoise 0.2.1 2014-04-22 CRAN (R 3.2.2)
## nycflights13 * 0.1 2014-07-22 CRAN (R 3.2.2)
## R6 2.1.2 2016-01-26 CRAN (R 3.2.2)
## Rcpp 0.12.3 2016-01-10 CRAN (R 3.2.2)
## rmarkdown 0.9.5 2016-02-12 Github (rstudio/rmarkdown@b24f7e7)
## stringi 1.0-1 2015-10-22 CRAN (R 3.2.2)
## stringr 1.0.0 2015-04-30 CRAN (R 3.2.2)
## yaml 2.1.13 2014-06-12 CRAN (R 3.2.2)

非常感谢。

最佳答案

从 S4 的角度来看,还不清楚 tbl_df 是否真的是一个 data.frame。您可以使用 setOldClass 声明该关系,您必须在其中提供 tbl_df 对象的完整类名列表。这样做向 S4 解释“tbl_df”确实继承自 data.frame:

setOldClass(c("tbl_df", "tbl", "data.frame"))

关于r - 带有 S4 对象槽的 tbl_df,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35642191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com