You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
library(data.table)
d <- data.table(mtcars)
class(d)
# "data.table" "data.frame"
(1) Apply dplyr verbs directly on the data.table
class data.table is retained when using select
library(magrittr)
d %>% dplyr::select(hp, mpg) %>% class
# "data.table" "data.frame"
class data.table is not retained when using filter
d %>% dplyr::filter(hp > 100) %>% class
# "data.frame"
First, I realize that this most likely not be 'the correct' way of using dplyr verbs on a data.table (hence the dtplyr package), but this was the premise of the SO question. Still, the .data argument and Value are described in the same way in ?filter and ?select, so from this information only it's hard to tell why .data of class data.table is treated differently in the two functions.
(2) I suppose the correct dtplyr way would be to explicitly convert the data.table to a "data table tbl" using tbl_dt to retain class data.table:
library(dtplyr)
d %>% tbl_dt() %>% dplyr::select(hp, mpg) %>% class
# [1] "tbl_dt" "tbl" "data.table" "data.frame"
d %>% tbl_dt() %>% dplyr::filter(hp > 100) %>% class
# [1] "tbl_dt" "tbl" "data.table" "data.frame"
(3) However, merely loading dtplyr is enough to retain class data.table. Which was a bit unexpected.
d %>% dplyr::select(hp, mpg) %>% class
# [1] "data.table" "data.frame"
d %>% dplyr::filter(hp > 100) %>% class
# [1] "data.table" "data.frame"
The text was updated successfully, but these errors were encountered:
After answering How to retain data.table class when piped through dplyr::filter? on SO, I thought some observations could be worth mentioning here.
Create a
data.table
:(1) Apply
dplyr
verbs directly on thedata.table
class
data.table
is retained when usingselect
class
data.table
is not retained when usingfilter
First, I realize that this most likely not be 'the correct' way of using
dplyr
verbs on adata.table
(hence thedtplyr
package), but this was the premise of the SO question. Still, the.data
argument and Value are described in the same way in?filter
and?select
, so from this information only it's hard to tell why.data
of classdata.table
is treated differently in the two functions.(2) I suppose the correct
dtplyr
way would be to explicitly convert the data.table to a "data table tbl" usingtbl_dt
to retain classdata.table
:(3) However, merely loading
dtplyr
is enough to retain classdata.table
. Which was a bit unexpected.The text was updated successfully, but these errors were encountered: