我想要爲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