3

我想为 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
4

6 回答 6

4

只是一个警告,如果您不指定 dplyr's 的顺序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

nthRfast 中的函数默认排序:

> 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 版本是通过使用部分排序来实现良好扩展的,这对于基于sort,orderrank(包括dplyr::nth) 的解决方案是不正确的。

于 2017-01-17T05:31:22.920 回答
1

这是我的例子;

num_func <- function(x, n) nth(sort(x), n)
sapply(dfchord, num_func, n = 2)  # edited (thanks for @thelatemail's comment)
于 2017-01-17T03:35:23.647 回答
1

既然你已经喜欢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
于 2017-01-17T03:37:21.013 回答
1

如果您正在寻找 and 的更快替代方案nth, CRAN 中的“kit”包中提供了sorttopn功能。请查看文档。

于 2020-05-20T06:38:25.613 回答
0

因此,这是在任何 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)
于 2017-01-17T03:29:33.450 回答
0

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
于 2017-01-17T06:40:20.990 回答