R中data.frame中每列的第N个最小值

Nth smallest value for every column in data.frame in R

我想为 data.frame 中的每一列找到第 n 个最小的数字。

在下面的示例中,我实际上使用 dcast nth 函数指定了第二小的值。有人可以帮助编写函数的代码吗?

library(vegclust)
library(dplyr)
data(wetland)
dfnorm = decostand(wetland,"normalize")
dfchord = dist(dfnorm, method = "euclidean")
dfchord = data.frame(as.matrix(dfchord)
number_function = function(x) nth(x,2) # can change 2 to any number..

answer_vector = apply(dfchord, 2, number) # here, 2 specifying apply on columns

实际答案应该是这样的..

ans = c(0.5689322,0.579568297,0.315017693,0.315017693,0.632246369, 0.868563003, 0.704638684, 0.35827587, 0.725220337, 0.516397779) # length of 1:38

所以这是在任何 data.frame 的列中获取任何第 n 个值的答案,您只需要更改 y[x] 中的 x。

x = dfchord

for (i in (1:ncol(x))) {
  y = sort(x[,i], decreasing=FALSE)
  ans$small[i] = y[2] # this is the second biggest number, replace the value with whatever you want
  ans$rel = rownames(x)
}

answer = data.frame( 'nth' = ans$small, 'rel' = ans$rel)

这是我的例子;

num_func <- function(x, n) nth(sort(x), n)
sapply(dfchord, num_func, n = 2)  # edited (thanks for @thelatemail's comment)

因为你已经喜欢 dplyr 这就是我现在与 purrr 一起做的事情:

purrr::map_dbl(mtcars, ~nth(., 2, order_by = .))
   mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear   carb 
10.400  4.000 75.700 62.000  2.760  1.615 14.600  0.000  0.000  3.000  1.000 

或仅 dplyr 因为它已经加载 nth():

summarise_all(mtcars, funs(nth(., 2, order_by = .))
   mpg cyl disp hp drat    wt qsec vs am gear carb
1 10.4   4 75.7 62 2.76 1.615 14.6  0  0    3    1

只是一个警告,如果你不指定 dplyr 的 nth() 的顺序,它实际上不会进行排序:

例如,

> sapply(mtcars, dplyr::nth, 2)
    mpg     cyl    disp      hp    drat      wt    qsec      vs      am    gear    carb 
 21.000   6.000 160.000 110.000   3.900   2.875  17.020   0.000   1.000   4.000   4.000 

这实际上只是数据的第二行:

> mtcars[2,]
              mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

Rfast 中的 nth 函数默认排序:

> sapply(mtcars, Rfast::nth, 2)
   mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear   carb 
10.400  4.000 75.700 62.000  2.760  1.615 14.600  0.000  0.000  3.000  1.000 

如果您对性能敏感,Rfast 版本被编写为通过使用部分排序很好地扩展,这不适用于基于 sortorder 或 [=17 的解决方案=](包括dplyr::nth)。

dplyr::summarize_each

n <- 2
dfchord %>% summarize_each(funs(nth(sort(.),n)))
#          X5        X8       X13        X4       X17       X3        X9       X21       X16       X14        X2       X15        X1        X7
# 1 0.5689322 0.5795683 0.3150177 0.3150177 0.6322464 0.868563 0.7046387 0.3582759 0.7252203 0.5163978 0.3651484 0.5163978 0.3582759 0.4222794
#         X10      X40       X23       X25       X22      X20        X6      X18      X12       X39       X19       X11       X30       X34
# 1 0.4222794 0.507107 0.6206017 0.4536844 0.4536844 0.654303 0.5126421 0.338204 0.338204 0.5126421 0.5393651 0.5804794 0.7270723 0.5242481
#        X28       X31       X26       X29       X33       X24       X36      X37       X41       X27       X32       X35       X38
# 1 0.735765 0.5242481 0.7270723 0.8749704 0.5715592 0.4933355 0.4933355 0.574123 0.7443697 0.6333863 0.6333863 0.7296583 0.6709442

如果您正在寻找 nthsort 的更快替代品,CRAN 中的包 "kit" 中有 topn 函数。请查看文档。