[R] removing values from a vector, where both the value and its name are the same?
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Fri Jun 15 21:41:16 CEST 2007
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.
More information about the R-help
mailing list