【发布时间】:2017-01-17 02:23:26
【问题描述】:
我想为 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
【问题讨论】:
-
这听起来有点奇怪。为了让您和您的同事将来更容易阅读,您可能需要melt 然后split-apply-combine