相关疑难解决方法(0)

dplyr在data.table上,我真的在使用data.table吗?

如果我使用dplyr语法在上面的数据表,做我得到的数据表中的所有速度优势,同时仍然使用dplyr的语法?换句话说,如果我使用dplyr语法查询数据表,是否会误用数据表?或者我是否需要使用纯数据表语法来利用它的所有功能.

提前感谢任何建议.代码示例:

library(data.table)
library(dplyr)

diamondsDT <- data.table(ggplot2::diamonds)
setkey(diamondsDT, cut) 

diamondsDT %>%
    filter(cut != "Fair") %>%
    group_by(cut) %>%
    summarize(AvgPrice = mean(price),
                 MedianPrice = as.numeric(median(price)),
                 Count = n()) %>%
    arrange(desc(Count))
Run Code Online (Sandbox Code Playgroud)

结果:

#         cut AvgPrice MedianPrice Count
# 1     Ideal 3457.542      1810.0 21551
# 2   Premium 4584.258      3185.0 13791
# 3 Very Good 3981.760      2648.0 12082
# 4      Good 3928.864      3050.5  4906
Run Code Online (Sandbox Code Playgroud)

这是我想出的数据表等价.不确定它是否符合DT良好做法.但我想知道代码是否比场景背后的dplyr语法更有效:

diamondsDT [cut != "Fair"
        ] [, .(AvgPrice = mean(price),
                 MedianPrice = as.numeric(median(price)),
                 Count = .N), by=cut …
Run Code Online (Sandbox Code Playgroud)

r dplyr data.table

83
推荐指数
3
解决办法
1万
查看次数

标签 统计

data.table ×1

dplyr ×1

r ×1