【问题标题】:combining two vectors in R into one with NAs not removed [closed]将 R 中的两个向量合并为一个,其中未删除 NA [关闭]
【发布时间】:2013-12-13 12:09:30
【问题描述】:

我有两个 xts 对象:

buypoints
2010-04-20 7.096034  
2010-04-22 7.097276  
2010-06-02 7.001592  
2010-06-10 6.991030  
2010-07-09 6.982826  
2010-08-27 6.970345



sell points  
2010-04-16 7.083497  
2010-04-21 7.095015  
2010-04-27 7.076409  
2010-06-04 6.970617  
2010-06-24 6.978857  
2010-08-05 7.026258  
2010-09-27 7.040676  
2010-10-28 7.076468

我想合并这两个对象,这样我的最终输出看起来像:

2010-04-16 7.083497  
2010-04-20 7.096034   
2010-04-21 7.095015  
2010-04-22 7.097276   
2010-04-27 7.076409  
2010-06-02 7.001592   
2010-06-04 6.970617  
2010-06-10 6.991030    
2010-06-24 6.978857  
2010-07-09 6.982826    
2010-08-05 7.026258  
2010-08-27 6.970345    
2010-09-27 7.040676  
2010-10-28 7.076468

也就是说,来自两个对象的数据仅在one column 中按时间顺序排列。 请提出一个这样做的方法

【问题讨论】:

  • 请显示您尝试过的代码。干杯。

标签: r merge xts


【解决方案1】:

你可以试试这样的:

rbind(buypoints, sellpoints)
# [,1]
# 2010-04-16 7.083497
# 2010-04-20 7.096034
# 2010-04-21 7.095015
# 2010-04-22 7.097276
# 2010-04-27 7.076409
# 2010-06-02 7.001592
# 2010-06-04 6.970617
# 2010-06-10 6.991030
# 2010-06-24 6.978857
# 2010-07-09 6.982826
# 2010-08-05 7.026258
# 2010-08-27 6.970345
# 2010-09-27 7.040676
# 2010-10-28 7.076468
# > class(rbind(buypoints, sellpoints))
# [1] "xts" "zoo"

看起来xts 已经预见到您需要按日期进行排序,因此这里避免了任何显式排序功能。很整洁,嗯?

【讨论】:

    猜你喜欢
    • 2019-08-13
    • 2014-08-23
    • 2020-09-28
    • 2015-12-30
    • 2014-04-15
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多