Re: [R] removing values from a vector, where both the value and its name are the same?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri, 15 Jun 2007 21:41:16 +0200

Patrick Burns wrote:
> In case it matters, the given solution has a problem if the
> data look like:
>
> x <- c(sum=77, test=99, sum=99)
>
> By the description all three elements should be kept, but
> the duplicated solution throws out the last element. A more
> complicated solution is:
>
> unique(data.frame(x, names(x)))
>
> (and then put the vector back together again).
>
>
Yes, I was about to say the same.

x[!duplicated(cbind(x,names(x)))]

looks like it might cut the mustard.



R-help_at_stat.math.ethz.ch 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 Fri 15 Jun 2007 - 21:25:17 GMT

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 Fri 15 Jun 2007 - 21:32:31 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.