我想要为data.frame中的每列找到第n个最小值。R中的data.frame中每列的第N个最小值
在下面的示例中,我使用dcast第n个函数实际指定了第二小的值。有人可以帮助编码的功能?
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
听起来有点陌生的事情。为了让您和您的同事在将来更易于阅读,您可能需要[解散](http://seananderson.ca/2013/10/19/reshape.html),然后[split-apply-combine]( http://stackoverflow.com/questions/26664644/use-dplyrs-group-by-per-perform-split-apply-combine) – citynorman