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