Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:41:58+00:00 2026-06-09T13:41:58+00:00

I was happily running with this code: z=lapply(filename_list, function(fname){ read.zoo(file=fname,header=TRUE,sep = ,,tz = )

  • 0

I was happily running with this code:

z=lapply(filename_list, function(fname){
    read.zoo(file=fname,header=TRUE,sep = ",",tz = "")
    })
xts( do.call(rbind,z) )

until Dirty Data came along with this at the end of one file:

                        Open     High      Low    Close Volume
2011-09-20 21:00:00 1.370105 1.370105 1.370105 1.370105      1

and this at the start of the next file:

                        Open     High      Low  Close Volume
2011-09-20 21:00:00 1.370105 1.371045 1.369685 1.3702   2230

So rbind.zoo complains about a duplicate.

I can’t use something like:

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

as they are in different zoo objects, inside a list. And I cannot use aggregate, as suggested here because they are a list of zoo objects, not one big zoo object. And I can’t get one big object ‘cos of the duplicates. Catch-22.

So, when the going gets tough, the tough hack together some for loops (excuse the prints and a stop, as this isn’t working code yet):

indexes <- do.call("c", unname(lapply(z, index)))
dups=duplicated(indexes)
if(any(dups)){
    duplicate_timestamps=indexes[dups]
    for(tix in 1:length(duplicate_timestamps)){
        t=duplicate_timestamps[tix]
        print("We have a duplicate:");print(t)
        for(zix in 1:length(z)){
            if(t %in% index(z[[zix]])){
                print(z[[zix]][t])
                if(z[[zix]][t]$Volume==1){
                    print("-->Deleting this one");
                    z[[zix]][t]=NULL    #<-- PROBLEM
                    }
                }
            }
        }
    stop("There are duplicate bars!!")
    }

The bit I’ve got stumped on is assigning NULL to a zoo row doesn’t delete it (Error in NextMethod(“[<-“) : replacement has length zero). OK, so I do a filter-copy, without the offending item… but I’m tripping up on these:

> z[[zix]][!t,]
Error in Ops.POSIXt(t) : unary '!' not defined for "POSIXt" objects

> z[[zix]][-t,]
Error in `-.POSIXt`(t) : unary '-' is not defined for "POSIXt" objects

P.S. While high-level solutions to my real problem of “duplicates rows across a list of zoo objects” are very welcome, the question here is specifically about how to delete a row from a zoo object given a POSIXt index object.


A small bit of test data:

list(structure(c(1.36864, 1.367045, 1.370105, 1.36928, 1.37039, 
1.370105, 1.36604, 1.36676, 1.370105, 1.367065, 1.37009, 1.370105, 
5498, 3244, 1), .Dim = c(3L, 5L), .Dimnames = list(NULL, c("Open", 
"High", "Low", "Close", "Volume")), index = structure(c(1316512800, 
1316516400, 1316520000), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo"), 
    structure(c(1.370105, 1.370115, 1.36913, 1.371045, 1.37023, 
    1.37075, 1.369685, 1.36847, 1.367885, 1.3702, 1.36917, 1.37061, 
    2230, 2909, 2782), .Dim = c(3L, 5L), .Dimnames = list(NULL, 
        c("Open", "High", "Low", "Close", "Volume")), index = structure(c(1316520000, 
    1316523600, 1316527200), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo"))

UPDATE: Thanks to G. Grothendieck for the row-deleting solution. In the actual code I followed the advice of Joshua and GSee to get a list of xts objects instead of a list of zoo objects. So my code became:

z=lapply(filename_list, function(fname){
    xts(read.zoo(file=fname,header=TRUE,sep = ",",tz = ""))
    })
x=do.call.rbind(z)

(As an aside, please note the call to do.call.rbind. This is because rbind.xts has some serious memory issues. See https://stackoverflow.com/a/12029366/841830 )

Then I remove duplicates as a post-process step:

dups=duplicated(index(x))
if(any(dups)){
    duplicate_timestamps=index(x)[dups]
    to_delete=x[ (index(x) %in% duplicate_timestamps) & x$Volume<=1]
    if(nrow(to_delete)>0){
        #Next line says all lines that are not in the duplicate_timestamp group
        #     OR are in the duplicate timestamps, but have a volume greater than 1.
        print("Will delete the volume=1 entry")
        x=x[ !(index(x) %in% duplicate_timestamps) | x$Volume>1]
        }else{
        stop("Duplicate timestamps, and we cannot easily remove them just based on low volume.")
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:41:59+00:00Added an answer on June 9, 2026 at 1:41 pm

    If z1 and z2 are your zoo objects then to rbind while removing any duplicates in z2:

    rbind( z1, z2[ ! time(z2) %in% time(z1) ] )
    

    Regarding deleting points in a zoo object having specified times, the above already illustrates this but in general if tt is a vector of times to delete:

    z[ ! time(z) %in% tt ]
    

    or if we knew there were a single element in tt then z[ time(z) != tt ] .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this code building and running perfectly: boost::function<void(string)> bar = boost::bind(&Bar::BarHandler, this, _1);
I receive an SqlException when running the following code. Procedure or function 'usp_Search' expects
I have a script that is supposed to sit there, happily running in a
I have a PHP app running happily on the following system: web app: PHP
There I was happily debugging my code, when I did a restart, and even
Eariler I happily used the following code for creating form elements (inside Zend_Form descendant):
an svn:externals reference like this dojo -r 21434 http://svn.dojotoolkit.org/src/dojo/trunk/ is happily accepted by subversive
Part of our product is an IE plugin (BHO), which is running happily in
I've been happily running a Regex replaceAllIn for quite a while but ran into
I had a running EC2 server with an elastic IP address that was happily

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.