Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中排序数据_R_Sorting_Dataframe - Fatal编程技术网

在R中排序数据

在R中排序数据,r,sorting,dataframe,R,Sorting,Dataframe,我有一个数据框,R中有900000行和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

我有一个数据框,R中有900000行和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
  • 伊苏

  • 级别的顺序在这里很重要,也就是说,它们应该首先按日期排序,如果有相同的日期,它们应该按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
    
    >测向
    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
    >带(df,df[订单(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”函数可以将多个向量作为参数

    在前面的解决方案的基础上,这里还有另外两种方法。第二种方法需要plyr

    df.sorted = df[do.call(order, df[names(df)]),];
    df.sorted = arrange(df, a, b, c) 
    

    如果上述答案都没有点燃你的火焰,你可以始终使用doBy软件包中的功能:

    require(doBy)
    sortedData <- orderBy(~date+mcode+ycode+yissue , data=unsortedData)
    
    require(多比)
    sortedData附加说明:使用-c()反转排序因子或字符列

    with(df, df[order(a, b, -c(myCharCol)), ])
    
    此外,还可以添加向量以仅拾取某些列

    with(df, df[order(a, b, c), c('a','b','x','y')])
    

    您还可以在参数前面加上-to-sort升序,而不是降序,例如,order(df$b,-df$a,df$c)。阅读
    帮助(sort)
    的第一段回答您的问题。在得到下面的答案后,我确信我做了正确的事情。我♥ 堆栈溢出。