[R] Finding Source of Error Message of 'Non-Unique Index Entries'

Rich Shepard rshepard at appl-ecosys.com
Tue Jan 3 23:53:04 CET 2012


On Tue, 3 Jan 2012, David Winsemius wrote:

> Maybe we need to backtrack a bit.

   Yes. I've been trying to do this but still have too little experience with
R to be successful on my own.

> You originally were complaining about an error that said you had
> duplicated index entries as you attempted to make a zoo object. I assumed,
> incorrectly it now appears, that you understood that an index in a zoo
> object was a vector. You now seem to be admitting that you were trying to
> use an entire dataframe as your index. As the acronym goes,
> ...  DDT.

   My understanding, apparently incorrect, was that read.zoo() converted a
data frame to a zoo object with the date column as the index vector. From
the read.zoo help page:

     file: character string or strings giving the name of the file(s) which
           the data are to be read from/written to. ... ‘file’ can be a
           ‘connection’ or a ‘data.frame’ (e.g., resulting from a previous
           ‘read.table’ call) that is subsequently processed to a ‘"zoo"’
           series.

   Based on this, I created a subset for a single parameter:

burns.tds <- subset(chemdata, stream == 'BurnsCrk', select = c(site,
sampdate, param == 'TDS', quant), drop = T)

This provides three columns:

      site   sampdate quant
599  BC-3 1992-03-27   0.1
600  BC-3 1992-04-30   0.1
601  BC-3 1992-05-30   0.1
603  BC-3 1992-06-19   0.1
1214 BC-3 1992-07-20   0.1
1215 BC-3 1992-08-10   0.1

   Then, to create the zoo object,

burns.tds.z <- read.zoo(burns.tds, split = 1, index = 2)


> Right. If you had said it was a dataframe, I would have suggested:
>
> burns.tds[ !duplicated(burns.tds) ,  ]
>
> But that would only identify entire duplicated rows; it would  not cure the 
> misguided notion of creating a zoo-index from a dataframe.

   Where did I go astray in trying to create a zoo-index with this procedure?
How do I extract zoo objects from a data frame?

> You still have not really described what your are trying to do ... or with 
> what data you are trying to do it with.

   I have a data frame with water quality sampling data and I'm now trying to
plot time series of specific chemical concentrations (y axis) as a function
of their irregular collection over a period of 30 years or less. I want to
plot the time series for each site along the stream as a separate line in
the same panel.

> You might want to think about taking that sampdate which is now a factor
> and turinging it into a data object which would then satisfy the
> requirements of an index for a zoo object.

   I had run the read.table() function again and forgot to convert the date
column from a factor to a date. I've now done this but the result is still
the same:

str(burns.tds)
'data.frame':	2472 obs. of  3 variables:
  $ site    : Factor w/ 137 levels "BC-0.5","BC-1",..: 5 5 5 5 5 5 5 5 5 5
  $ sampdate: Date, format: "1992-03-27" "1992-04-30" ...
  $ quant   : num  0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 8.08 ...

burns.tds.z <- read.zoo(burns.tds, split = 1, index = 2)
Error in merge.zoo(C-0.5 = c(0, 0.01, 0.01, 0.06, 0.18, NA, 76.56,  :
   series cannot be merged with non-unique index entries in a series
In addition: Warning messages:
1: In zoo(rval4[[i]], ix[[i]]) :
   some methods for “zoo” objects do not work if the index entries in
‘order.by’ are not unique

   I want to learn how to get data into zoo objects for time series analyses
so I greatly appreciate the help provided here.

Rich



More information about the R-help mailing list