如何使用R对具有不同列数的两个数据集进行行绑定

时间:2015-01-29 08:58:57

标签: r merge rbind

我正在尝试使用不同列数对两个xts数据集进行行绑定:

数据集1:

    one <- structure(c(0, 0.009009, 0.008929, -0.00885, 0, -0.017857, -0.027957, 
-0.00885, -0.013393, -0.024887, 0.00232, -0.009259, 0, 0, 0, 
0, 0, 0, -0.017794, 0.028986, -0.007143, 0.007194, 0.021429, 
0.017483, 0, 0, 0, 0, 0, 0, 0.007968, -0.011858, 0, -0.032, -0.008264, 
0.045833, 0.015924, 0.00627, -0.003115, 0, 0.00625, 0.024845), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXt", "POSIXct"), tclass = c("POSIXt", 
"POSIXct"), tzone = "", index = c(346406400, 346492800, 346665600, 
346924800, 347011200, 347097600), .Dim = 6:7, .Dimnames = list(
    NULL, c("ALLEGHENY.POWER.SYSTEMS.INC", "ALLIED.CHEMICAL.CORP", 
    "APPLICATION.ENGR.CORP", "ALLIS.CHALMERS.CORP", "AMERICAN.ELECTR.LABS.INC", 
    "A.E.L.INDUSTRIES.INC", "AMAX.INC")))

数据集2

   two <-  structure(c(0, 0, 0, 0, 0, 0, 0.071429, 0.066667, 0, -0.125, 
    0, 0, 0.018182, 0.026786, 0, 0.008696, -0.025862, -0.017699, 
    0.009346, 0.006944, 0.011494, -0.045455, -0.028571, 0.014706, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = c("xts", "zoo"), .indexCLASS =  c("POSIXt", 
    "POSIXct"), tclass = c("POSIXt", "POSIXct"), tzone = "", index = c(347270400, 
    347529600, 347616000, 347702400, 347788800, 347875200), .Dim = c(6L, 
    6L), .Dimnames = list(NULL, c("A.C.S.ENTERPRISE.INC", "A.C.S.INDUSTRIES.INC", 
    "ALLEGHENY.POWER.SYSTEMS.INC", "ALLIED.CHEMICAL.CORP", "ALLIED.CORP", 
    "ALLIED.SIGNAL.INC")))

我尝试了rbind(one,two, by=colnames(one)),但收到以下错误:

Error in rbind(deparse.level, ...) : 
  data must have same number of columns to bind by row

基本上我想绑定xts&amp;使用0添加其他列 处理缺失的列。

期望的输出:

DES <- structure(c(0, 0.009009, 0.008929, -0.00885, 0, -0.017857, 0.018182, 
0.026786, 0, 0.008696, -0.025862, -0.017699, -0.027957, -0.00885, 
-0.013393, -0.024887, 0.00232, -0.009259, 0.009346, 0.006944, 
0.011494, -0.045455, -0.028571, 0.014706, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, -0.017794, 0.028986, -0.007143, 0.007194, 0.021429, 
0.017483, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0.007968, -0.011858, 0, -0.032, -0.008264, 0.045833, 0, 0, 
0, 0, 0, 0, 0.015924, 0.00627, -0.003115, 0, 0.00625, 0.024845, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.071429, 0.066667, 0, -0.125, 0, 0, 0, 0, 0, 0, 0, 
0, 0.009346, 0.006944, 0.011494, -0.045455, -0.028571, 0.014706, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(12L, 11L), .Dimnames = list(
    NULL, c("ALLEGHENY.POWER.SYSTEMS.INC", "ALLIED.CHEMICAL.CORP", 
    "APPLICATION.ENGR.CORP", "ALLIS.CHALMERS.CORP", "AMERICAN.ELECTR.LABS.INC", 
    "A.E.L.INDUSTRIES.INC", "AMAX.INC", "A.C.S.ENTERPRISE.INC", 
    "A.C.S.INDUSTRIES.INC", "ALLIED.CHEMICAL.CORP.1", "ALLIED.SIGNAL.INC"
    )), index = c(346406400, 346492800, 346665600, 346924800, 
347011200, 347097600, 347270400, 347529600, 347616000, 347702400, 
347788800, 347875200), class = c("xts", "zoo"), .indexCLASS = c("POSIXt", 
"POSIXct"), tclass = c("POSIXt", "POSIXct"), tzone = "")

2 个答案:

答案 0 :(得分:3)

一种方法是创建具有适当尺寸的新矩阵(&#34; m1&#34;),即。 &{34; m1&#34; nrow将是&#34; one&#34;和&#34; two&#34;的行的总和,同样,ncol是两个数据集中所有唯一列的长度。创建&#39; name&#39;索引(&#39; onenm&#39;,&#39; twonm&#39;)专门存在于一个数据集中,或两个数据集中的唯一列名称(&#39; nm2&#39;)或常见名称在两者中(&#39; nm1&#39;)。通过使用适当的行/列&#39; index,我们可以分配来自&#39; one&#39;,&#39; two&#39;数据集到新创建的xts数据集(&#34; xt1&#34;从&#34; m1&#34;创建)。

nm1 <- intersect(colnames(one), colnames(two))
onenm <-  setdiff(colnames(one), colnames(two))
twonm <- setdiff(colnames(two), colnames(one))
nm2 <- union(colnames(one), colnames(two))
m1 <- matrix(0, nrow=nrow(one)+nrow(two), ncol=length(nm2), 
           dimnames=list(NULL, nm2))
xt1 <- xts(m1, order.by=c(index(one), index(two)))
xt1[index(one), onenm] <- one[,onenm]
xt1[index(two), twonm] <- two[,twonm]
xt1[,nm1] <- rbind(one[,nm1], two[,nm1])
dim(xt1)
#[1] 12 11

更新

您还可以使用rbindlist中的data.table(或来自bind_rows的{​​{1}})。将dplyr个对象转换为&#34; data.frame&#34;,将其放入列表中,并使用带有xts选项的rbindlist。将输出(&​​#39; dt1&#39;)转换为fill=TRUE(&#39; xt1&#39;),更改&#34; NA&#34;值为&#34; 0&#34;。

xts

答案 1 :(得分:1)

除了akrun的优秀答案之外,我在这里分享一个我用来执行两个xts的强大rbind的函数:

rbind.ordered=function(x,y){

  if (is.null(x)) return(y)

  if (is.null(y)) return(x)

  diffCol = setdiff(colnames(x),colnames(y))
  if (length(diffCol)>0){
    cols=colnames(y)
    for (i in 1:length(diffCol)) y=cbind(y,NA)
    colnames(y)=c(cols,diffCol)
  }

  diffCol = setdiff(colnames(y),colnames(x))
  if (length(diffCol)>0){
    cols=colnames(x)
    for (i in 1:length(diffCol)) x=cbind(x,NA)
    colnames(x)=c(cols,diffCol)
  }
  return(rbind(x, y[, colnames(x)]))
}

rbind.ordered(one, two)

然后你只需将NA替换为0就可以得到你想要的