【问题标题】:skimr: how to get the top 3 and bottom 3 values?skimr:如何获得前 3 和后 3 的值?
【发布时间】:2019-09-11 13:00:38
【问题描述】:

考虑这个简单的例子

> tibble(value = c(1,2,3,4,5,5,6,7,8,9,10,11,12)) %>%
+   skim()
Skim summary statistics
 n obs: 13 
 n variables: 1 

-- Variable type:numeric -------------------------------------------------------
 variable missing complete  n mean   sd p0 p25 p50 p75 p100     hist
    value       0       13 13 6.38 3.48  1   4   6   9   12 ▅▂▇▂▂▅▂▅

我只需将topbottom 两列添加到skimr 输出,它们显示前3 个和后3 个值,用逗号分隔。

类似

top        bottom
12,11,10   1,2,3

我该怎么做? 谢谢!

【问题讨论】:

  • 看看skim_with()函数。
  • 我无法让它工作。它对你有用吗?谢谢!
  • 我没试过,但是根据文档,你可以定义你的统计数据集。所以我想你也可以定义获取顶部/底部 3 个值。

标签: r dplyr skimr


【解决方案1】:

更新答案:

#remove the p values and histogram for space to work with
skim_with(numeric = list(p0 = NULL, p25 = NULL, p50=NULL, p75 = NULL, p100=NULL, hist=NULL))

#6 functions, for head 1 2 and 3, and tail 3 2 and 1.
h1<-function(x){head(sort(x))[1]}
h2<-function(x){head(sort(x))[2]}
h3<-function(x){head(sort(x))[3]}
t3<-function(x){tail(sort(x),3)[1]}
t2<-function(x){tail(sort(x),2)[1]}
t1<-function(x){tail(sort(x),1)[1]}

#assign those functions to return for numeric (need to do the same for integer and others)
skim_with(numeric = list(h1=h1, h2=h2, h3=h3, t3=t3, t2=t2, t1=t1))
skim(iris$Sepal.Length)
Skim summary statistics

── Variable type:numeric ────────────────────────────────────────────────
          variable missing complete   n mean   sd  h1  h2  h3  t3  t2  t1
 iris$Sepal.Length       0      150 150 5.84 0.83 4.3 4.4 4.4 7.7 7.7 7.9

【讨论】:

    【解决方案2】:

    好的,我能够让它工作。 供将来参考:

    get_top <- function(df) {
      df %>% as_tibble() %>% 
        top_n(3) %>% 
        pull() %>% 
        paste(collapse = ',')
    }
    
    skim_with(numeric = list(top = get_top), append = TRUE)
    

    给予

    > tibble(value = c(1,2,3,4,5,5,6,7,8,9,10,11,12)) %>%
    +   skim()
    Selecting by value
    Skim summary statistics
     n obs: 13 
     n variables: 1 
    
    -- Variable type:numeric -------------------------------------------------------
     variable missing complete  n mean   sd p0 p25 p50 p75 p100     hist      top
        value       0       13 13 6.38 3.48  1   4   6   9   12 ▅▂▇▂▂▅▂▅ 10,11,12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多