R 如何绑定两个数据帧的行,使ID列中具有相同值的行彼此相邻

R 如何绑定两个数据帧的行,使ID列中具有相同值的行彼此相邻,r,dataframe,R,Dataframe,我有两个数据帧,看起来像: 数据1.1: cust_id stats AIRLINE AUTO RENTAL CLOTHING STORES 1: 495 SUM 45493.42 3103.90 20927.56 2: 692 SUM 39954.78 0.00 20479.60 3: 728 SUM 25813.03 3504.74

我有两个数据帧,看起来像:

数据1.1:

          cust_id stats  AIRLINE AUTO RENTAL CLOTHING STORES
   1:         495   SUM 45493.42     3103.90        20927.56
   2:         692   SUM 39954.78        0.00        20479.60
   3:         728   SUM 25813.03     3504.74         5924.71
   4:        1794   SUM     0.00        0.00            0.00
   5:        3060   SUM     0.00        0.00         7146.31
数据2.2:

         cust_id stats AIRLINE AUTO RENTAL CLOTHING STORES
   1:         495   MAX 4950.00     1000.00            3140
   2:         692   MAX 6479.71        0.00            1880
   3:         728   MAX 5642.68     1752.37            1395
   4:        1794   MAX    0.00        0.00               0
   5:        3060   MAX    0.00        0.00            1338
我希望将它们绑定在一起(按行),以便生成的数据帧如下所示:

           cust_id stats AIRLINE AUTO RENTAL CLOTHING STORES
   1:         495   SUM   45493.42     3103.90        20927.56
   2:         495   MAX   4950.00      1000.00        3140
   3:         692   SUM   39954.78     0.00           20479.60
   4:         692   MAX   6479.71      0.00           1880
   5:         728   SUM   25813.03     3504.74        5924.71
   6:         728   MAX   5642.68      1752.37        1395
   .
   .
   .
这意味着,来自两个数据帧的具有相同
cust_id
的行在绑定的数据帧中彼此相邻


提前感谢您的时间。

只需使用
rbind
将它们绑定在一起,然后使用
order
对数据帧进行排序:

mydata <- rbind(data1, data2)

mydata[order(mydata$cust_id), ]

mydata也许dplyr中的arrange函数会有用:

custid <- c(111,222,333)
otherVar <- c(1,2,3)

df1 <- data.frame(custid, otherVar)

custid <- c(222,333,444)
otherVar <- c(2,3,4)

df2 <- data.frame(custid, otherVar)

df <- df1 %>%
  bind_rows(df2) %>%
  arrange(custid)
custid寻找不同的选项来合并数据帧与dplyr,
bind_行(data.one,data.two)%%>%arrange(cust_id,-stats)