【问题标题】:How to create a column with a quartile rank?如何创建具有四分位数的列?
【发布时间】:2011-09-22 00:26:04
【问题描述】:

我在 R 中有一个名为 tableOne 的表,如下所示:

idNum        binaryVariable        salePrice
2               1                    55.56
4               0                    88.33
15              0                     4.45
87              1                    35.77
...            ...                    ...

我想使用以下产生的值:summary(tableOne$salePrice) 来按 salePrice 创建四个四分位数。然后,我想创建一个 tableOne$quartile 列,其中每行 salePrice 所在的四分位数。它看起来像:

idNum        binaryVariable            salePrice      quartile
    2               1                    55.56            3
    4               0                    88.33            4
    15              0                     4.45            1
    87              1                    35.77            2 
    ...            ...                    ...            ...  

有什么建议吗?

【问题讨论】:

    标签: r


    【解决方案1】:

    应该这样做:

    tableOne <- within(tableOne, quartile <- as.integer(cut(salesPrice, quantile(salesPrice, probs=0:4/4), include.lowest=TRUE)))
    

    ...一些细节:

    within 函数非常适合计算新列。您不必将列称为 tableOne$salesPrice

    tableOne <- within(tableOne, quartile <- <<<some expression>>>)
    

    quantile 函数计算分位数(或在您的情况下为四分位数)。 0:4/4 计算结果为 c(0, 0.25, 0.50, 0.75, 1)

    最后,cut 函数将您的数据拆分为这些四分位数。但是你会得到一个名字很奇怪的factor,所以as.integer 把它变成了组1,2,3,4

    尝试?within 等以了解有关此处提到的功能的更多信息...

    【讨论】:

    • 除了更改为integer,还可以使用cutlabel 选项。使用非常四分位数(或分位数)作为标签使其明确了类的限制。在这种情况下,..., label = quantile(salesPrice, probs=0:4/4)[2:5]。需要去掉一个标签(零)。
    【解决方案2】:

    data.table 方法

        library(data.table)
        tableOne <- setDT(tableOne)[, quartile := cut(salesPrice, quantile(salesPrice, probs=0:4/4), include.lowest=TRUE, labels=FALSE)]
    

    【讨论】:

      【解决方案3】:

      使用 dplyr 你可以使用 ntile 函数:

      ntile(x, n)
      
      
      tableOne$quartile <- ntile(tableOne$salesPrice, 4)
      

      这将在表格中添加一列,为每行分配一个基于 n 的分位数及其所在的价格分位数。

      注意:此方法从较低的值 1 开始,然后从那里向上工作。

      【讨论】:

      • ntile() 不分成四分位等级,它把数据分成n 大小相等的桶。
      【解决方案4】:

      cut() 中设置参数labels=FALSE 以整数形式返回类别名称。见?cut

      tableOne <- within(tableOne, quartile <- cut(salesPrice, quantile(salesPrice, probs=0:4/4), include.lowest=TRUE, labels=FALSE))
      

      【讨论】:

        【解决方案5】:

        您可以使用以下脚本

        tableOne$Quartile<-ifelse(tableOne$salesPrice<=quantile(tableOne$salesPrice,c(0.25)),1,
                                   ifelse(tableOne$salesPrice<=quantile(tableOne$salesPrice,c(0.5)),2,
                                          ifelse(tableOne$salesPrice<=quantile(tableOne$salesPrice,c(0.75)),3,
                                                 ifelse(tableOne$salesPrice<=quantile(tableOne$salesPrice,c(1)),4,NA))))
        

        【讨论】:

        • 虽然这可行,但效率低下,因为它会重新计算每个级别的分位数。而且它显然不能很好地扩大规模......如果你想要十分位数而不是四分位数,那会很烦人。如果您想要百分位数而不是四分位数,那将是一团糟。
        【解决方案6】:

        使用包cutr 我们可以做到:

        # devtools::install_github("moodymudskipper/cutr")
        library(cutr)
        df$quartile <- smart_cut(df$salePrice, 4, "g", output = "numeric")
        #   idNum binaryVariable salePrice quartile
        # 1     2              1     55.56        3
        # 2     4              0     88.33        4
        # 3    15              0      4.45        1
        # 4    87              1     35.77        2
        

        【讨论】:

          【解决方案7】:

          以下代码创建一个 ntile 组向量:

          qgroup = function(numvec, n = 4){
          
              qtile = quantile(numvec, probs = seq(0, 1, 1/n))
              out = sapply(numvec, function(x) sum(x >= qtile[-(n+1)]))
          
              return(out)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-01
            • 2019-02-11
            • 1970-01-01
            • 1970-01-01
            • 2019-08-04
            • 2019-10-14
            • 2018-12-10
            • 2019-06-18
            相关资源
            最近更新 更多