Re: [R] Remove duplicate elements in lists via recursive indexing

From: Timothy Bates <timothy.c.bates_at_gmail.com>
Date: Mon, 23 May 2011 13:23:44 +0100

Dear Janko,
I think requires a for loop. The approach I took here was mark the dups, then dump them all in one hit:

testData = expand.grid(letters[1:4],c(1:3)) testData$keep=F
uniqueIDS = unique(testData$Var1)
for(thisID in uniqueIDS) {

	firstCaseOnly = match(thisID,testData$Var1)
	testData[firstCaseOnly,"keep"]=T

}

(testData = testData[testData$keep==T,])

On 23 May 2011, at 11:59 AM, Janko Thyson wrote:

> 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.



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 - 12:26:04 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 - 16:20:09 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