从xts对象中删除重复的行 [英] Remove duplicate rows from xts object

查看:84
本文介绍了从xts对象中删除重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法删除xts对象中的重复行.我有一个R脚本,它将下载某种货币的报价财务数据并将其转换为OHLC格式的xts对象.该脚本还每15分钟提取一次新数据.从今天的第一笔交易到今天的最后记录的交易下载新数据.以前下载的旧数据以.Rdata格式存储并调用.然后将新数据添加到旧数据中,并以.Rdata格式覆盖旧数据.

I am having trouble deleting duplicated rows in an xts object. I have a R script that will download tick financial data of a currency and convert it to an xts object of OHLC format. The script also pulls new data every 15 minutes. The new data is downloaded from the first trade of today to the last recorded trade of today. The old previous data downloaded was stored in .Rdata format and called. Then the new data is added to the old data and it overwrites the old data in .Rdata format.

以下是我的数据的示例:

Here is an example of what my data looks like:

                      .Open   .High    .Low  .Close   .Volume .Adjusted
2012-01-07 00:00:11 6.69683 7.01556 6.38000 6.81000  48387.58   6.81000
2012-01-08 00:00:09 6.78660 7.20000 6.73357 7.11358  57193.53   7.11358
2012-01-09 00:00:57 7.08362 7.19100 5.81000 6.32570 148406.85   6.32570
2012-01-10 00:01:01 6.32687 6.89000 6.00100 6.36000 110210.25   6.36000
2012-01-11 00:00:07 6.44904 7.13800 6.41266 6.90000  99442.07   6.90000
2012-01-12 00:01:02 6.90000 6.99700 6.33700 6.79999 140116.52   6.79999
2012-01-13 00:02:01 6.78211 6.80400 6.40000 6.41000  60228.77   6.41000
2012-01-14 00:00:23 6.42000 6.50000 6.23150 6.31894  25392.98   6.31894

现在,如果我再次运行脚本,我会将新数据添加到xts中.

Now if I run the script again I will add the new data to the xts.

                      .Open   .High    .Low  .Close   .Volume .Adjusted
2012-01-07 00:00:11 6.69683 7.01556 6.38000 6.81000  48387.58   6.81000
2012-01-08 00:00:09 6.78660 7.20000 6.73357 7.11358  57193.53   7.11358
2012-01-09 00:00:57 7.08362 7.19100 5.81000 6.32570 148406.85   6.32570
2012-01-10 00:01:01 6.32687 6.89000 6.00100 6.36000 110210.25   6.36000
2012-01-11 00:00:07 6.44904 7.13800 6.41266 6.90000  99442.07   6.90000
2012-01-12 00:01:02 6.90000 6.99700 6.33700 6.79999 140116.52   6.79999
2012-01-13 00:02:01 6.78211 6.80400 6.40000 6.41000  60228.77   6.41000
2012-01-14 00:00:23 6.42000 6.50000 6.23150 6.31894  25392.98   6.31894
2012-01-14 00:00:23 6.42000 6.75000 6.22010 6.57157  75952.01   6.57157

您可以看到最后一行与倒数第二行是同一天.我想保留最后一行作为最后一个日期,并删除倒数第二个.当我尝试使用以下代码删除重复的行时,该行不起作用,重复的行留在那里.

As you can see the last line is the same day as the second to last line. I want to keep the last row for the last date and delete the second to last row. When I try the following code to delete duplicated rows it does not work, the duplicated rows stay there.

xx <- mt.xts[!duplicated(mt.xts$Index),]
xx
.Open .High .Low .Close .Volume .Adjusted

我没有任何结果.如何使用索引作为重复指标删除xts对象中的重复数据条目?

I do not get any result. How can I delete duplicate data entries in an xts object using the Index as the indicator of duplication?

推荐答案

不是index(mt.xts)而不是mt.xts$Index吗? 以下似乎有效.

Should't it be index(mt.xts) rather than mt.xts$Index? The following seems to work.

# Sample data
library(xts)
x <- xts( 
  1:10, 
  rep( seq.Date( Sys.Date(), by="day", length=5 ), each=2 ) 
)

# Remove rows with a duplicated timestamp
y <- x[ ! duplicated( index(x) ),  ]

# Remove rows with a duplicated timestamp, but keep the latest one
z <- x[ ! duplicated( index(x), fromLast = TRUE ),  ]

这篇关于从xts对象中删除重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆