【问题标题】:R In multi-summarise function, quantile not showing result properlyR在多汇总函数中,分位数未正确显示结果
【发布时间】:2021-05-08 15:55:40
【问题描述】:

我使用 data.table 为不同的列按组生成多个函数。代码如下:

library(data.table)
# samle data
x = rnorm(30)
y = rnorm(30)+10
group = c(rep(1,11),rep(2,19))
dt = data.table(x,y,group)

my.summary = function(x) list(length(x), 
                              sum(is.na(x)), 
                              round(mean(x),3), 
                              round(sd(x)/sqrt(length(x)),3),
                              round(sd(x),3),
                              round(min(x),3),
                              round(quantile(x,0.25),3),
                              round(median(x),3),
                              round(quantile(x,0.75),3),
                              round(max(x),3)
                              )
dt_des = dt[, lapply(.SD, my.summary), .SDcols = colnames(df),by=group]

除了显示的分位数函数外,一切正常: “c(25% = -0.508)” “c(25% = 9.654)” “c(75% = 0.48)” "c(75% = 10.675)"

谁能帮我解决这个问题?谢谢!

【问题讨论】:

  • 最后一行显示为:dt_des = dt[, lapply(.SD, my.summary), .SDcols = c("x", "y"),by=group]
  • 使用round(unname(quantile(x,0.25),3)) 删除向量的名称。在这个例子中colnames(df) 应该是colnames(dt) 吗?

标签: r quantile


【解决方案1】:

只需unname quantile 函数:

my.summary = function(x) list(length(x), 
                              sum(is.na(x)), 
                              round(mean(x),3), 
                              round(sd(x)/sqrt(length(x)),3),
                              round(sd(x),3),
                              round(min(x),3),
                              round(unname(quantile(x,0.25)),3),
                              round(median(x),3),
                              round(unname(quantile(x,0.75)),3),
                              round(max(x),3)
)

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多