【问题标题】:List of Lists to a merged matrix [duplicate]合并矩阵的列表列表[重复]
【发布时间】:2016-05-12 22:09:02
【问题描述】:

很抱歉,我想在 R 中尝试做的事情,在你们看来是一个非常基本的问题。

我的数据是一个列表列表。

dataset 
$Series1
date      Value
2015-11-01 1.301
2015-11-02 6.016
2015-11-03 4.871
2015-11-04 10.925
2015-11-05 7.638

$Series2
date      Value
2015-11-01 1.532
2015-11-02 3.730
2015-11-03 6.910
2015-11-04 3.554
2015-11-05 2.631

关于如何转换为关注者有什么想法吗?

datamatrix
date      Series1 Series2
2015-11-01 1.301  1.532
2015-11-02 6.016  3.730
2015-11-03 4.871  6.910
2015-11-04 10.925 3.554
2015-11-05 7.638  2.631

【问题讨论】:

  • 你确定这是一个列表列表吗?
  • 是的,但这有关系吗?

标签: r zoo


【解决方案1】:

您可以使用Reduce 合并时间序列

dataset = list(Series1=read.table(text="
date      Value
2015-11-01 1.301
2015-11-02 6.016
2015-11-03 4.871
2015-11-04 10.925
2015-11-05 7.638",header=TRUE,stringsAsFactors=FALSE),Series2=read.table(text="
date      Value
2015-11-01 1.532
2015-11-02 3.730
2015-11-03 6.910
2015-11-04 3.554
2015-11-05 2.631",header=TRUE,stringsAsFactors=FALSE))

mergeFun = function(x,y) merge(x,y,by="date") 

datamatrix = Reduce(mergeFun,dataset)
colnames(datamatrix) = c("date",names(dataset))

datamatrix
#       date Series1 Series2
# 2015-11-01   1.301   1.532
# 2015-11-02   6.016   3.730
# 2015-11-03   4.871   6.910
# 2015-11-04  10.925   3.554
# 2015-11-05   7.638   2.631

对多个系列使用xts 库

我在数据集中添加了额外的列,使用 xts 您可以执行以下操作

require(xts)

dataset = list(Series1=read.table(text="
date      Value
2015-11-01 1.301
2015-11-02 6.016
2015-11-03 4.871
2015-11-04 10.925
2015-11-05 7.638",header=TRUE,stringsAsFactors=FALSE),Series2=read.table(text="
date      Value
2015-11-01 1.532
2015-11-02 3.730
2015-11-03 6.910
2015-11-04 3.554
2015-11-05 2.631",header=TRUE,stringsAsFactors=FALSE),Series3=read.table(text="
date      Value
2015-11-01 1
2015-11-02 3
2015-11-03 6
2015-11-04 3
2015-11-05 2",header=TRUE,stringsAsFactors=FALSE),Series4=read.table(text="
date      Value
2015-11-01 1.1
2015-11-02 3.2
2015-11-03 6.3
2015-11-04 3.4
2015-11-05 2.5",header=TRUE,stringsAsFactors=FALSE))


datasetXTS = lapply(dataset,function(x) {
z=xts(x[,-1],order.by=as.Date(x[,1],format="%Y-%m-%d"));
colnames(z) = tail(colnames(x),1);
z 
})

datamatrix = Reduce(merge,datasetXTS)

datamatrix
#            Value Value.1 Value.2 Value.3
#2015-11-01  1.301   1.532       1     1.1
#2015-11-02  6.016   3.730       3     3.2
#2015-11-03  4.871   6.910       6     6.3
#2015-11-04 10.925   3.554       3     3.4
#2015-11-05  7.638   2.631       2     2.

系列已正确合并,但由于您在所有系列中具有相同的列名,因此它们被重复。要解决这个问题:

colnames(datamatrix) = names(dataset)

datamatrix
#           Series1 Series2 Series3 Series4
#2015-11-01   1.301   1.532       1     1.1
#2015-11-02   6.016   3.730       3     3.2
#2015-11-03   4.871   6.910       6     6.3
#2015-11-04  10.925   3.554       3     3.4
#2015-11-05   7.638   2.631       2     2.5

【讨论】:

  • 感谢您的回复。如果列表中的系列超过 3 个,此方法不起作用?
  • 它适用于列表中的任何数字系列,只要它们具有相同的公共列
  • 警告信息:在 merge.data.frame(x, y, by = "date") 中:列名 'Value.x', 'Value.y' 在结果中重复 这是如果有超过 3 个系列,我会收到错误。
  • 让我知道新解决方案是否适合您
  • 效果很好,非常感谢
猜你喜欢
  • 2017-05-03
  • 2014-02-09
  • 2021-08-23
  • 2019-12-06
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
相关资源
最近更新 更多