【问题标题】:How to rowbind two datasets with different number of columns using R如何使用 R rbind 具有不同列数的两个数据集
【发布时间】:2015-03-28 10:24:29
【问题描述】:

我正在尝试对具有不同列数的两个 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 进行行绑定并使用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 = "")

【问题讨论】:

  • 您的列名已关闭。你想 rbind 他们 asis 吗?还是 rbind 共享列名的列?
  • 你好 @Avinash 我想将它们与共享相同列名的那些进行 rbind。
  • @akrun 当merge.xts() 时,我得到一个合并side by sidexts ...在此示例中,onetwo 共享的唯一名称是:@987654334 @ & ALLIED.CHEMICAL.CORP. 如果我 merge.xts() 它将返回一个包含 13 列的对象,而所需的只有 11 列(因为它们共享两个常见的标题名称)
  • @Rime 请检查以下解决方案。我不明白11 列。你只有两列是通用的。
  • @Rime 我可能错过了你的目标。你能显示预期的输出吗

标签: r merge rbind


【解决方案1】:

一种方法是创建一个具有适当尺寸的新矩阵(“m1”),即。 "m1" 的 nrow 将是 "one" 和 "two" 的行的总和,同样,ncol 是两个数据集中所有唯一列的长度。创建仅存在于一个数据集中的“名称”索引(“onenm”,“twonm”),或两个数据集中的唯一列名(“nm2”),或两者中通用的名称(“nm1”)。通过使用适当的“行/列”索引,我们可以将“一”、“二”数据集中的元素分配给新创建的xts 数据集(“xt1”从“m1”创建)。

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

更新

您也可以使用来自data.tablerbindlist(或来自dplyrbind_rows)。将xts 对象转换为“data.frame”,将其放在一个列表中并使用rbindlistfill=TRUE 选项。将输出 ('dt1') 转换为 xts ('xt1'),将“NA”值更改为“0”。

 library(data.table)
 dt1 <- rbindlist(list(as.data.frame(one),
                   as.data.frame(two)), fill=TRUE)
 #or
 #library(dplyr)
 #dt1 <- bind_rows(list(as.data.frame(one), as.data.frame(two)))
 xt2 <- xts(dt1, order.by=c(index(one), index(two)))
 xt2[is.na(xt2)] <- 0
 identical(xt1, xt2)
 #[1] TRUE

【讨论】:

  • 这适用于仅绑定常用名称。我怎样才能得到其余的列
【解决方案2】:

除了 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 即可获得您想要的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2015-04-18
    • 2018-05-02
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多