dplyr动词删除自定义类和属性? [英] dplyr verbs removing custom classes and attributes?
问题描述
我正在使用一个依赖于将类和属性添加到数据框的包,并希望能够使用 dplyr
动词。
唯一的麻烦是他们似乎剥夺了我添加到我的数据框架的类和属性。
示例
class(mtcars)< - c(new_class mtcars))
attr(mtcars,foo)< - bar
检查结构显示,mtcars现在包括新的类和属性
> mtcars%>%str
/ pre>
Classes'new_class','new_class'和'data.frame':32 obs。 11个变量:
$ mpg:num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl:num 6 6 4 6 8 6 8 4 4 6 ...
$ disp:num 160 160 108 258 360 ...
$ hp:num 110 110 93 110 175 105 245 62 95 123 ...
$ drat:num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt:num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec:num 16.5 17 18.6 19.4 17 ...
$ vs:num 0 0 1 1 0 1 0 1 1 1 ...
$ am:num 1 1 1 0 0 0 0 0 0 0 ...
$ gear:num 4 4 4 3 3 3 3 4 4 4 ...
$ carb:num 4 4 1 1 2 1 4 2 2 4 ...
- attr(*,foo)= chrbar
但是当我使用
过滤器
时,似乎失去了类和属性。> mtcars%>%filter(cyl == 8)%>%str
'data.frame':14 obs。 11个变量:
$ mpg:num 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 ...
$ cyl:num 8 8 8 8 8 8 8 8 8 8 ...
$ disp:num 360 360 276 276 276 ...
$ hp:num 175 245 180 180 180 205 215 230 150 150 ...
$ drat:num 3.15 3.21 3.07 3.07 3.07 2.93 3 3.23 2.76 3.15 ...
$ wt:num 3.44 3.57 4.07 3.73 3.78 ...
$ qsec:num 17 15.8 17.4 17.6 18 ...
$ vs:num 0 0 0 0 0 0 0 0 0 0 ...
$ am:num 0 0 0 0 0 0 0 0 0 0 ...
$ gear:num 3 3 3 3 3 3 3 3 3 3 ...
$ carb:num 2 4 3 3 3 4 4 4 2 2 ...
是那个行为有什么预期?
我可以做些什么来解决?
解决方案code> filter 调用
filter _
,它使用data.frame的方法
(因为new_class
没有过滤器方法。filter_.data.frame
然后使用调用过滤器方法tbl_df
并使用as.data.frame
返回一个data.frame
。dplyr ::: filter_.data.frame
##函数(.data,.. 。,.dots)
## {
## dots < - lazyeval :: all_dots(.dots,...,all_named = TRUE)
## as.data.frame( filter_(tbl_df(.data),.dots = dots))
##}
##< environment:namespace:dplyr>
胁迫到
tbl_df
删除额外的类,但保留属性foo
。mtcars%>%tbl_df%>%str
过滤似乎松动了属性
foo
。mtcars%>%tbl_df%>%filter (cyl == 8)%>%str
I'm working on a package that relies on adding a class and attributes to a data frame, and would like to be able to use
dplyr
verbs with it.The only trouble is they seem to strip away the classes and attributes that I've added to my data frames.
Example
class(mtcars) <- c("new_class", class(mtcars)) attr(mtcars, "foo") <- "bar"
Examining the structure shows that mtcars now includes the new class and attributes
> mtcars %>% str Classes ‘new_class’, ‘new_class’ and 'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ... - attr(*, "foo")= chr "bar"
But when I use
filter
, it seems to lose the classes and attributes.> mtcars %>% filter(cyl == 8) %>% str 'data.frame': 14 obs. of 11 variables: $ mpg : num 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 ... $ cyl : num 8 8 8 8 8 8 8 8 8 8 ... $ disp: num 360 360 276 276 276 ... $ hp : num 175 245 180 180 180 205 215 230 150 150 ... $ drat: num 3.15 3.21 3.07 3.07 3.07 2.93 3 3.23 2.76 3.15 ... $ wt : num 3.44 3.57 4.07 3.73 3.78 ... $ qsec: num 17 15.8 17.4 17.6 18 ... $ vs : num 0 0 0 0 0 0 0 0 0 0 ... $ am : num 0 0 0 0 0 0 0 0 0 0 ... $ gear: num 3 3 3 3 3 3 3 3 3 3 ... $ carb: num 2 4 3 3 3 4 4 4 2 2 ...
Is that behaviour expected?
What can I do to work around it?
解决方案
filter
callsfilter_
, which uses the method for adata.frame
(because there is no filter method fornew_class
.filter_.data.frame
then uses calls the filter method for atbl_df
and usesas.data.frame
to return adata.frame
.dplyr:::filter_.data.frame ## function (.data, ..., .dots) ## { ## dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) ## as.data.frame(filter_(tbl_df(.data), .dots = dots)) ## } ## <environment: namespace:dplyr>
The coercion to
tbl_df
removes the extra class, but keeps the attributefoo
.mtcars %>% tbl_df %>% str
The filtering seems to loose the attribute
foo
.mtcars %>% tbl_df %>% filter(cyl == 8) %>% str
这篇关于dplyr动词删除自定义类和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!