dplyr动词删除自定义类和属性? [英] dplyr verbs removing custom classes and attributes?

查看:91
本文介绍了dplyr动词删除自定义类和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个依赖于将类和属性添加到数据框的包,并希望能够使用 dplyr 动词。



唯一的麻烦是他们似乎剥夺了我添加到我的数据框架的类和属性。



示例

  class(mtcars)<  -  c(new_class mtcars))
attr(mtcars,foo)< - bar

检查结构显示,mtcars现在包括新的类和属性

 > mtcars%>%str 
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
/ pre>

但是当我使用过滤器时,似乎失去了类和属性。

 > 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 calls filter_, which uses the method for a data.frame (because there is no filter method for new_class. filter_.data.frame then uses calls the filter method for a tbl_df and uses as.data.frame to return a data.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 attribute foo.

mtcars %>% tbl_df %>% str

The filtering seems to loose the attribute foo.

mtcars %>% tbl_df %>% filter(cyl == 8) %>% str

这篇关于dplyr动词删除自定义类和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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