gpt4 book ai didi

r - 在 R 中对数据进行排序

转载 作者:行者123 更新时间:2023-12-04 16:21:44 27 4
gpt4 key购买 nike

我在 R 中有一个包含 900,000 行和 11 列的数据框。列名和类型如下:

column name: date / mcode / mname / ycode / yname / yissue  / bsent   / breturn / tsent   / treturn / csales
type: Date / Char / Char / Char / Char / Numeric / Numeric / Numeric / Numeric / Numeric / Numeric

我想按以下顺序按这些变量对数据进行排序:
  • 日期
  • mcode
  • ycode
  • yissue

  • 级别的顺序在这里很重要,即它们应该先按日期排序,如果有相同的日期,它们应该按 mcode 排序,依此类推。我怎么能在 R 中做到这一点?

    最佳答案

    也许像这样?

    > df<- data.frame(a=rev(1:10), b=rep(c(2,1),5), c=rnorm(10))
    > df
    a b c
    1 10 2 -0.85212079
    2 9 1 -0.46199463
    3 8 2 -1.52374565
    4 7 1 0.28904717
    5 6 2 -0.91609012
    6 5 1 1.60448783
    7 4 2 0.51249796
    8 3 1 -1.35119089
    9 2 2 -0.55497745
    10 1 1 -0.05723538
    > with(df, df[order(a, b, c), ])
    a b c
    10 1 1 -0.05723538
    9 2 2 -0.55497745
    8 3 1 -1.35119089
    7 4 2 0.51249796
    6 5 1 1.60448783
    5 6 2 -0.91609012
    4 7 1 0.28904717
    3 8 2 -1.52374565
    2 9 1 -0.46199463
    1 10 2 -0.85212079

    “order”函数可以接受多个向量作为参数。

    关于r - 在 R 中对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4087998/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com