[R] Remove duplicate elements in lists via recursive indexing

Janko Thyson janko.thyson.rstuff at googlemail.com
Mon May 23 12:59:39 CEST 2011


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



More information about the R-help mailing list