【问题标题】:Sort columns in order of decreasing variance in R按 R 中方差递减的顺序对列进行排序
【发布时间】:2022-11-27 01:26:04
【问题描述】:

我希望按照减少列方差的顺序对数据集的列进行排序,但我没有这样做。这是我到目前为止所拥有的:

og_data <- og_data[, sort(apply(og_data, 2, var), decreasing=TRUE)]

现在,我知道这是行不通的,因为 sort(apply(og_data, 2, var), decreasing=TRUE) 按方差递减的顺序返回列的方差值。我不知道如何从中提取列索引,这是我需要使用的。任何帮助将非常感激。

【问题讨论】:

  • 您需要 order 而不是 sort

标签: r dataframe


【解决方案1】:

由于您没有提供可重现的数据供我使用 你可以试试下面这个方法

# sorting examples using the mtcars dataset
attach(mtcars)

# sort by mpg
new_data <- mtcars[order(mpg),]

# sort by mpg and cyl
new_data <- mtcars[order(mpg, cyl),]

#sort by mpg (ascending) and cyl (descending)
new_data <- mtcars[order(mpg, -cyl),]

希望这解决了你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2013-08-07
    • 2014-11-29
    相关资源
    最近更新 更多