Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Skimer:如何获得前3名和后3名的值?_R_Dplyr_Skimr - Fatal编程技术网

Skimer:如何获得前3名和后3名的值?

Skimer:如何获得前3名和后3名的值?,r,dplyr,skimr,R,Dplyr,Skimr,考虑这个简单的例子 > 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

考虑这个简单的例子

> 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 ▅▂▇▂▂▅▂▅
我只需在Skimer输出中添加两列
top
bottom
,这两列显示了前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)

好的,我能让它工作。 供日后参考:

get_top <- function(df) {
  df %>% as_tibble() %>% 
    top_n(3) %>% 
    pull() %>% 
    paste(collapse = ',')
}

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

看看
skim_with()
函数。我无法让它工作。对你有用吗?谢谢我没有尝试过,但是根据文档,您可以定义统计数据集。因此,我假设您还可以定义以获得top/bottom3值。
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