考虑这个简单的例子

> 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

我怎样才能做到这一点?
谢谢!

最佳答案

更新的答案:

#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

关于r - Skimr:如何获取前3名和后3名值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57889803/

10-12 17:47