简体   繁体   English

按行/表合并具有不同列长/名称的表

[英]Merging tables by row/ rbind tables with different column lengths/names by row

I've many tables containing different ARIMA-Orders from a time series analysis. 我有很多表,其中包含来自时间序列分析的不同ARIMA订单。 A short example would be something like this: The two tables are Order1 and Order2 containing a) different orders b) different frequencies of combinations. 一个简短的示例将是这样的:两个表是Order1和Order2,包含a)不同的订单b)不同的组合频率。

Order1
(1,0,1) (1,1,1) (2,1,4)
     4       5       9

Order2
(1,0,1) (3,0,4) (0,1,1) (2,1,2)
    1        2       7       10

In the previous step the two tables are sorted by sort(Order1) and sort(Order2) 在上一步中,两个表按sort(Order1)和sort(Order2)排序

I want to merge both tables, by row to get one "big" table with two rows. 我想按行合并两个表,以获得一个包含两行的“大”表。 My expected Output should be something like this: 我的预期输出应该是这样的:

          (1,0,1) (1,1,1)  (2,1,4) (3,0,4) (0,1,1) (2,1,2)
Order 1        4       5        9       0      0        0
Order 2        1       0        0       2      7        10

or if its possible just the two input tables in one big table with "different column names and lentghs" 或者,如果可能的话,只需将一个大表中的两个输入表包含“不同的列名和长度”

        (1,0,1) (1,1,1) (2,1,4)
Order 1      4       5       9 
        (1,0,1) (3,0,4) (0,1,1) (2,1,2)
Order 2        1     2       7      10

I tried something like merge or rbind, but it doesn't work. 我尝试了诸如merge或rbind之类的操作,但不起作用。

dd1 <- as.data.table(t1)
dd2 <- as.data.table(t2)
merge(dd1, dd2, by="Var1", incomparables=NA, all=TRUE)

Is it OK ? 可以吗?

> merge(dd1,dd2, by="Var1", incomparables = NA, all=TRUE)
                 Var1 Freq.x Freq.y
1 ( 2 1 1 ) ( 1 1 1 )      1     NA
2 ( 1 1 0 ) ( 1 1 1 )      3     NA
3 ( 0 1 1 ) ( 0 1 1 )     29     93
4 ( 2 1 1 ) ( 0 1 1 )     54      2
5 ( 1 1 0 ) ( 0 1 1 )     58     NA
6 ( 2 1 0 ) ( 0 1 1 )     NA     54

To have the frequencies in rows: 要使频率成行显示:

> ddd <- merge(dd1, dd2, ...) # as above
> ddd <- dd[,-1]
> rownames(ddd) <- dd$Var1
> t(ddd)
       ( 2 1 1 ) ( 1 1 1 ) ( 1 1 0 ) ( 1 1 1 ) ( 0 1 1 ) ( 0 1 1 )
Freq.x                   1                   3                  29
Freq.y                  NA                  NA                  93
       ( 2 1 1 ) ( 0 1 1 ) ( 1 1 0 ) ( 0 1 1 ) ( 2 1 0 ) ( 0 1 1 )
Freq.x                  54                  58                  NA
Freq.y                   2                  NA                  54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM