R设置do.call上的列名(rbind)

R设置do.call上的列名(rbind),r,xts,R,Xts,总之,我希望向XTS对象添加一个累积卷列。但是,在调用do.call(rbind…时,我发现原始XTS被覆盖 # Reproducible example data foo <- rnorm(5) bar <- seq(as.Date("1970-01-01"), length = 5, by = "days") foobar <- xts(x = foo, order.by = bar) names(foobar)[1] <- "Volume" # My process

总之,我希望向XTS对象添加一个累积卷列。但是,在调用
do.call(rbind…
时,我发现原始XTS被覆盖

# Reproducible example data
foo <- rnorm(5)
bar <- seq(as.Date("1970-01-01"), length = 5, by = "days")
foobar <- xts(x = foo, order.by = bar)
names(foobar)[1] <- "Volume"
# My processing ...
foobar_months <- split(foobar[,"Volume"],f="months")
foobar_vol_mtd <- lapply(foobar_months,FUN=cumsum)
# This is what is not working for me because Volume overwrites original Volume
foobar <- do.call(rbind,foobar_vol_mtd) 
#可复制的示例数据
foo函数
do.call(rbind,list)
将对所有列表元素执行rbind。您不会将该列表附加到原始列表中。您可以做的是:

foobar2 <- do.call(rbind,foobar_vol_mtd)
foobar <- rbind(foobar, foobar2)
由于
rnorm(5)
和无种子集,结果会有所不同

追加为新列

正如我所说,
rbind
追加新行,所有列应相同。如果要追加为新列,请尝试:

foobar2 <- do.call(rbind,foobar_vol_mtd)
foobar3 = merge(foobar, foobar2)
然后用
名称(foobar)[2]=“新名称”
更改列名

您还可以在合并之前重命名:

foobar2 <- do.call(rbind,foobar_vol_mtd)
names(foobar2) = 'newname'
foobar3 = merge(foobar, foobar2)

foobar2对不起,我不清楚到底是什么问题?代码的哪一部分表现得不符合预期?@RonakShah我想将cumsum卷与原始XTS表合并。但是“新”卷会覆盖XTS。我只想要一个新列。如果你想要“新”列列,
rbind
不是答案。
rbind
附加行。可能使用
cbind
merge
@R.Schifini我尝试过合并,但无法在合并时确定如何重命名?对不起,我有点R新手。谢谢,我会尝试。但如何避免合并时的命名冲突?
                Volume  Volume.1
1970-01-01  1.96291153 1.9629115
1970-01-02 -0.41771710 1.5451944
1970-01-03 -0.08827657 1.4569179
1970-01-04 -0.57243569 0.8844822
1970-01-05 -0.06093953 0.8235426
foobar2 <- do.call(rbind,foobar_vol_mtd)
names(foobar2) = 'newname'
foobar3 = merge(foobar, foobar2)