【问题标题】:Can I split describe() on a factor variable? [duplicate]我可以在因子变量上拆分 describe() 吗? [复制]
【发布时间】:2020-07-15 12:14:33
【问题描述】:

我想根据一个因子变量中的所有值来描述一个响应变量。

我想运行类似这样的代码

library("Hmisc")
describe(mtcars$hp)

除了我想通过cyl的每个值得到不同的输出

【问题讨论】:

    标签: r hmisc


    【解决方案1】:

    tidy/purrr 解决方案

    library(Hmisc)
    library(purrr)
    mtcars %>%
      split(.$cyl) %>%
      purrr::map(~ describe(.x$hp))
    #> $`4`
    #> .x$hp 
    #>        n  missing distinct     Info     Mean      Gmd      .05      .10 
    #>       11        0       10    0.995    82.64    24.51     57.0     62.0 
    #>      .25      .50      .75      .90      .95 
    #>     65.5     91.0     96.0    109.0    111.0 
    #> 
    #> lowest :  52  62  65  66  91, highest:  93  95  97 109 113
    #>                                                                       
    #> Value         52    62    65    66    91    93    95    97   109   113
    #> Frequency      1     1     1     2     1     1     1     1     1     1
    #> Proportion 0.091 0.091 0.091 0.182 0.091 0.091 0.091 0.091 0.091 0.091
    #> 
    #> $`6`
    #> .x$hp 
    #>        n  missing distinct     Info     Mean      Gmd 
    #>        7        0        4    0.911    122.3    23.71 
    #>                                   
    #> Value        105   110   123   175
    #> Frequency      1     3     2     1
    #> Proportion 0.143 0.429 0.286 0.143
    #> 
    #> $`8`
    #> .x$hp 
    #>        n  missing distinct     Info     Mean      Gmd 
    #>       14        0        9    0.985    209.2    56.69 
    #> 
    #> lowest : 150 175 180 205 215, highest: 215 230 245 264 335
    #>                                                                 
    #> Value        150   175   180   205   215   230   245   264   335
    #> Frequency      2     2     3     1     1     1     2     1     1
    #> Proportion 0.143 0.143 0.214 0.071 0.071 0.071 0.143 0.071 0.071
    

    【讨论】:

    • 如果我想拆分两个变量,可以split(.$cyl, .$vs)
    • 在这种情况下,我会使用“新”dplyr group_split 像这样mtcars %>% group_by(cyl, am) %>% group_split() %>% purrr::map(~ describe(.x$hp))
    【解决方案2】:

    你在找吗

    lapply(split(mtcars,mtcars$cyl),describe)
    

    编辑:我看到你专门在hp 上寻找describe。您可以将$hp 添加到上述拆分中,或者更简单地使用

    tapply(mtcars$hp,mtcars$cyl,describe)
    

    【讨论】:

    • 秒杀我。除了在这个例子中似乎第一个 mtcars 应该是 mtcars$hp
    • 注意细节@Adam,我已经更新了答案
    • 我可以将其附加到 dplyr 声明中吗?我想做类似 mtcars %>% filter(hp > x) %>% {tapply function}
    • 你可以这样做@Cauder mtcars %>% filter(hp > 100) %>% split(.$cyl) %>% purrr::map(~ describe(.x$hp))
    【解决方案3】:

    您可以group_by cyl 并将describe 对象存储在列表中:

    library(dplyr)
    library(Hmisc)
    
    new_mtcars <- mtcars %>% group_by(cyl) %>% summarise(data = list(describe(hp)))
    new_mtcars
    # A tibble: 3 x 2
    #    cyl data      
    #  <dbl> <list>    
    #1     4 <describe>
    #2     6 <describe>
    #3     8 <describe>
    
    new_mtcars$data[[1]]
    #hp 
    #       n  missing distinct     Info     Mean      Gmd      .05      .10 
    #      11        0       10    0.995    82.64    24.51     57.0     62.0 
    #     .25      .50      .75      .90      .95 
    #    65.5     91.0     96.0    109.0    111.0 
    
    #lowest :  52  62  65  66  91, highest:  93  95  97 109 113
                                                                      
    #Value         52    62    65    66    91    93    95    97   109   113
    #Frequency      1     1     1     2     1     1     1     1     1     1
    #Proportion 0.091 0.091 0.091 0.182 0.091 0.091 0.091 0.091 0.091 0.091
    

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2021-11-21
      • 1970-01-01
      • 2021-11-30
      • 2017-03-12
      • 2012-10-30
      • 2014-07-11
      • 2022-01-15
      • 2020-01-22
      相关资源
      最近更新 更多