[R] Remove duplicate elements in lists via recursive indexing

From: Janko Thyson <janko.thyson.rstuff_at_googlemail.com>
Date: Mon, 23 May 2011 12:59:39 +0200


Dear list,

I'm trying to solve something pretty basic here, but I can't really come up with a good solution. Basically, I would just like to remove duplicated named elements in lists via a their respective recursive indexes (given that I have a routine that identifies these recursive indexes). Here's a little example:

# VECTORS
# Here, it's pretty simple to remove duplicated entries
y <- c(1,2,3,1,1)
idx.dupl <- which(duplicated(y))
y <- y[-idx.dupl]
# /

# LISTS

x <- list(a=list(a.1.1=1, a.1.1=2, a.1.1=3))

x[[c(1,1)]]
x[[c(1,2)]] # Should be removed.
x[[c(1,3)]] # Should be removed.

# Let's say a 'checkDuplicates' routine would give me:
idx.dupl <- list(c(1,2), c(1,3))

# Remove first duplicate:

x[[idx.dupl[[1]]]] <- NULL
x
# Problem:
# Once I remove the first duplicate, my duplicate index would have to be
# updated as well as there is not third element anymore.
x[[idx.dupl[[2]]]] <- NULL

# So something like this would not work:
sapply(idx.dupl, function(x.idx){

     x[[x.idx]] <<- NULL
})
# /

Sorry if I'm missing something totally obvious here, but do you have any idea how to solve this?

Thanks a lot,
Janko



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 23 May 2011 - 11:02:37 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 May 2011 - 12:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive