【问题标题】:re-order data after split in R在 R 中拆分后重新排序数据
【发布时间】:2013-12-14 14:33:37
【问题描述】:

我使用 split 功能将我的数据分组,结果如下:

$H1050
     row.names    Avg  Tree
1071      1904 2.8530 H1050
1072      1905 2.4030 H1050
1073      1906 1.6030 H1050
1074      1907 1.7660 H1050
1075      1908 2.5150 H1050

$H1040
     row.names    Avg  Tree
1737       1664 4.3200 H1040
1738       1665 4.0900 H1040
1739       1666 5.3100 H1040
1740       1667 4.8000 H1040
1741       1668 3.7000 H1040
1742       1669 4.0400 H1040
1743       1670 4.2800 H1040

总共大约有 36 个组。然后我循环遍历这些数据以绘制每个单独的数据组。我希望能够按 row.names 列中的元素数对拆分数据进行重新排序。例如,计算 row.names 列中元素的数量,然后数字最大的组应该在顶部并相应地降序。

这里最好的方法是什么?

非常感谢您的时间和精力!

【问题讨论】:

    标签: r split


    【解决方案1】:

    假设splat 是您的拆分数据集:

      splat[order(sapply(splat, function(x) length(x[["row.names"]])))]
    

    【讨论】:

      【解决方案2】:

      类似这样的东西(假设一个名为 l 的列表):

      num.row.names = lapply(l, function(x) length(unique(x$row.names)))
      num.row.names = do.call(c, num.row.names)
      l = l[order(num.row.names, decreasing=T)]
      

      如果 row.names 是拆分数据框中的行的名称,而不是数据框中的变量(我不清楚它来自 OP),您可以将第一行替换为:

      num.row.names = lapply(l, function(x) length(unique(row.names(x))))
      

      【讨论】:

        猜你喜欢
        • 2016-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        相关资源
        最近更新 更多