使用data.table进行聚合

时间:2013-12-27 04:16:58

标签: r data.table

我正在寻找一种使用data.table汇总和计算数值变量百分比的简单方法。 以下代码输出所需的结果,我的问题是,是否有更好的方法来获得相同的结果。我对包装并不熟悉,所以任何提示都会有用。

我想要以下列:

   second_factor_variable third_factor_variable factor_variable       porc porcentaje
1:                   HIGH                     C           > 200 0.04456544        4 %
2:                    LOW                     A        51 - 100 0.31739130       32 %
3:                    LOW                     A       101 - 200 0.68260870       68 %
4:                    LOW                     A         26 - 50 0.00000000        0 %

porc 是数值百分比, porcentage 是四舍五入的百分比,用作ggplot调用中的标签。

library("ggplot2")
library("scales")
library("data.table")

### Generate some data
set.seed(123)
df <- data.frame(x = rnorm(10000, mean = 100, sd = 50))
df <- subset(df, x > 0)

df$factor_variable <- cut(df$x, right = TRUE, 
                          breaks = c(0, 25, 50, 100, 200, 100000),
                          labels = c("0 - 25", "26 - 50", "51 - 100", "101 - 200", "> 200")
                          )

df$second_factor_variable <- cut(df$x, right = TRUE, 
                                 breaks = c(0, 100, 100000),
                                 labels = c("LOW", "HIGH")
                                 )

df$third_factor_variable <- cut(df$x, right = TRUE, 
                                 breaks = c(0, 50, 100, 100000),
                                 labels = c("A", "B","C")
                                )

str(df)

### Aggregate
DT <- data.table(df)
dt = DT[, list(factor_variable = unique(DT$factor_variable),
              porc = as.numeric(table(factor_variable)/length(factor_variable)),
              porcentaje = paste( round( as.numeric(table(factor_variable)/length(factor_variable), 0 ) * 100 ), "%")
              ), by="second_factor_variable,third_factor_variable"]

修改

我尝试过只使用一个变量进行agstudy的解决方案分组,我相信它对于生成标签(porcentaje列)并不起作用。在真实的数据集中,我最终遇到了类似的问题,我无法发现这个函数的错误。

grp <- function(factor_variable) {
  porc = as.numeric(table(factor_variable)/length(factor_variable))
  list(factor_variable = factor_variable[1],
       porc =porc,
       porcentaje = paste( round( porc, 0 ) * 100 , "%"))
}

DT[, grp(factor_variable) , by="second_factor_variable"]

数值正确

DT2 <- DT[DT$second_factor_variable %in% "LOW"]
table(DT2$factor_variable)/length(DT2$factor_variable)

我相信如果我将2个因子变量分组出现同样的问题:

DT[, grp(factor_variable) , by="second_factor_variable,third_factor_variable"]

2 个答案:

答案 0 :(得分:4)

2个更改:factorize porc 变量并且不使用DT来计算factor_variable

DT[, {   porc = as.numeric(table(factor_variable)/length(factor_variable))
         list(factor_variable = factor_variable[1],
               porc =porc,
               porcentaje = paste( round( porc, 0 ) * 100 , "%"))
        }
, by="second_factor_variable,third_factor_variable"]

答案 1 :(得分:0)

在真实数据集中出于某种原因,之前的功能效果不佳。价值观和标签不对。

我刚刚换了一点点就行了。我将它作为单独的答案添加,以便以后更容易找到。

这使得订单在创建时保持因子水平。以前的方式保留了他们在DT中的订单,因为我正在使用的特定数据不能正常工作。 标签现在有效,我的意思是porcentaje专栏。

factor_variable = levels(factor_variable) 

grp2 <- function(factor_variable) {
  porc = as.numeric(table(factor_variable)/length(factor_variable))
  list(factor_variable = levels(factor_variable), 
       porc = porc,
       porcentaje = paste( round( as.numeric(table(factor_variable)/length(factor_variable), 0 ) * 100 ), "%")
       )
}


DT[, grp2(factor_variable) , by="second_factor_variable"]
相关问题