mutate_if,summary_at等将data.table强制转换为data.frame [英] mutate_if, summarize_at etc coerce data.table to data.frame

查看:96
本文介绍了mutate_if,summary_at等将data.table强制转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有些 dplyr 函数,包括 mutate_if mutate_all mutate_at 等将data.table输入强制为data.frame。即使在?mutate_all 中进行了记录,这似乎也是一种奇怪的行为(在值下,它表示为 data.frame-但不会强制对数据进行微调。

It seems like some dplyr functions, including mutate_if, mutate_all, mutate_at etc coerce data.table inputs to data.frame. That seems like strange behaviour, even though it is documented in ?mutate_all (Under 'Value', it says 'data.frame' - but it does not coerce tibbles to data.frames.)

require(dplyr)
require(data.table)
data("iris")
dt <- as.data.table(iris)
class(dt)
#[1] "data.table" "data.frame"



class(mutate_if(dt, is.numeric, as.numeric))
#[1] "data.frame"

但是,这种动作不会发生:

However, this does not happen with tibbles:

tb <- as_tibble(iris)
class(tb)
#[1] "tbl_df"     "tbl"        "data.frame"



class(mutate_if(tb, is.numeric, as.numeric))
#[1] "tbl_df"     "tbl"        "data.frame"

是否有某种方法可以维护data.table,还是每次我使用其中之一来强制使用 as.data.table 作用域 mutate 函数?

Is there some way to maintain the data.table, or do i need to coerce with as.data.table every time I use one of the scoped mutate functions?

推荐答案

如果您想尝试其他方法,
我最近发布了 table.express 包,其中
使用很多 dplyr 和自定义动词来构建 data.table 表达式。

If you'd like to try an alternative, I recently released the table.express package, which uses many dplyr and custom verbs to build data.table expressions.

链接的插图提供了详细说明,
,但提供了一些示例:

The linked vignette provides detailed explanations, but some examples:

library(data.table)
library(table.express)

data("iris")
DT <- as.data.table(iris)

# mutate_all (modification by reference does not print)
DT %>%
  mutate_sd(everything(), as.integer)

# mutate_if
DT %>%
  mutate_sd(~ is.numeric(.x), as.integer)

# mutate_at
DT %>%
  mutate_sd(contains("."), ~ .x * 1.5)

# transmute_all
DT %>%
  transmute_sd(everything(), as.integer)

# transmute_if
DT %>%
  transmute_sd(~ is.numeric(.x), as.integer)

# transmute_at
DT %>%
  transmute_sd(contains("."), as.integer)

请注意, mutate_sd 会进行修改默认情况下,根据引用,
可以根据需要在示例之间重新定义 DT

Do note that mutate_sd modifies by reference by default, so re-define DT between examples if you like.

从0.3.0版本开始,
将无法同时加载 table.express dtplyr 同时,
因为它们为许多 dplyr 泛型定义了相同的 data.table 方法。

Also, as of version 0.3.0, you won't be able to load both table.express and dtplyr at the same time, since they define the same data.table methods for many dplyr generics.

这篇关于mutate_if,summary_at等将data.table强制转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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