【发布时间】:2017-07-31 13:43:30
【问题描述】:
我可以选择并排列单个列:
iris %>%
select(Petal.Width, Species) %>%
arrange(desc(Petal.Width))
但我想对整个数据框执行此操作。我正在使用 forloop 来解决这个问题:
features <- colnames(iris)
top <- data.frame()
for (i in 1:length(features)) {
label <- features[[i]]
iris %>%
select(label, Species) %>%
arrange(desc(label)) %>%
top_n(3) %>%
rbind(top)
}
# Error in arrange_impl(.data, dots) :
# incorrect size (1) at position 1, expecting : 150
这给了我一个错误。
显然arrange(desc(label)) 不起作用。我四处搜索并尝试使用UQ 和substitute 之类的方法来取消引用label,但没有结果。
rbind(top) 和 top_n 结尾可能也不是我想要的,但我现在遇到的主要问题是如何使用label,所以 forloop 会接受它。
也许有人知道比我的 forloop 更好的方法...
所需的输出是一个数据框,每列的前 3 个。
【问题讨论】: