Remove duplicate rows from xts object

后端 未结 2 523
轻奢々
轻奢々 2020-12-10 00:11

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 OHL

相关标签:
2条回答
  • 2020-12-10 00:42

    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 ),  ]
    
    0 讨论(0)
  • 2020-12-10 00:46

    In my case,

    x <- x[! duplicated( index(x) ),]
    

    did not work as intended, because the system somehow makes date-time unique in each row.

    x <- x[! duplicated( coredata(x) ),]
    

    This may work if the previous solution did not help.

    0 讨论(0)
提交回复
热议问题