【问题标题】:R: addition value from one table to another by date (join the tables)R:按日期从一个表到另一个表的附加值(加入表)
【发布时间】:2016-09-30 17:32:29
【问题描述】:

美好的一天!请帮我! 我将按日期将表“D”(“xts”“zoo”)中的值“Open”添加到表“d”(“data.frame”)(“D”中的“index”,“dateOpen”中“d”)。我想在“d”中添加一个具有相应日期的新列。我怎样才能用 R 做到这一点?

谢谢!

"d"
             dateOpen priceOpen           dateClose priceClose  res cumres
1 2016-01-13 11:55:00     70670 2016-01-13 12:46:00      69853 -817   -817
2 2016-01-13 16:20:00     70670 2016-01-13 18:31:00      69853 -817  -1634
3 2016-01-14 21:55:00     70090 2016-01-14 23:49:00      69940 -150  -1784

"D"
            Open  High   Low Close  Volume
2016-01-11 16811 73560 68230 68710  656176
2016-01-12 68700 70720 67800 69050  766788
2016-01-13 69130 70930 68140 68470  798137
2016-01-14 68420 70220 67980 70060  757285
2016-01-15 69970 69970 64020 64970 1147526




    The answer is:
            dateOpen priceOpen           dateClose priceClose  res cumres Open   
1 2016-01-13 11:55:00     70670 2016-01-13 12:46:00      69853 -817   -817 69130 
2 2016-01-13 16:20:00     70670 2016-01-13 18:31:00      69853 -817  -1634 69130 
3 2016-01-14 21:55:00     70090 2016-01-14 23:49:00      69940 -150  -1784 68420 

【问题讨论】:

  • 将一个表匹配到另一个表意味着您想要加入这些表。

标签: r datetime merge


【解决方案1】:

使用merge 函数。

df <- merge(d,
            D[, c("index", "Open")],
            by.x = "dateOpen",
            by.y = "index")

这假设您在 D 中的日期列被命名为“索引”,这部分在您的问题中有点不清楚。

【讨论】:

  • In D date 是时间序列的索引,没有任何名称。
  • 我理解这个想法。一切正常!非常感谢!
  • 很高兴我能帮上忙。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多