首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >skimr:如何得到前3和底部3的值?

skimr:如何得到前3和底部3的值?
EN

Stack Overflow用户
提问于 2019-09-11 13:00:38
回答 2查看 181关注 0票数 2

考虑一下这个简单的例子

代码语言:javascript
复制
> 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的值,并以逗号分隔。

有点像

代码语言:javascript
复制
top        bottom
12,11,10   1,2,3

我怎么能这么做?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-11 15:16:22

好吧,我能让它运转起来。供日后参考:

代码语言:javascript
复制
get_top <- function(df) {
  df %>% as_tibble() %>% 
    top_n(3) %>% 
    pull() %>% 
    paste(collapse = ',')
}

skim_with(numeric = list(top = get_top), append = TRUE)

给出

代码语言:javascript
复制
> 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
票数 2
EN

Stack Overflow用户

发布于 2019-09-11 14:42:56

最新答复:

代码语言:javascript
复制
#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)

撇除汇总统计数据(────────────────────────────────────────────────)变量类型:缺少完整n个完全n的数值h1 h2 h3 t3 t2 t1 iris$Sepal.Length 0 150 5.84 0.83 0.83 4.3 4.4 4.4 7.7 7.7 7.9

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57889803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档