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

Patrick Burns pburns at pburns.seanet.com
Fri Jun 15 21:25:20 CEST 2007


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

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

jim holtman wrote:

>try this:
>
>  
>
>>x[!(duplicated(names(x))&duplicated(x))]
>>    
>>
>  sum   min   max count count  test
>   77     4     9     5     8    77
>
>
>
>On 6/15/07, new ruser <newruser at yahoo.com> wrote:
>  
>
>>I have an array such as:
>>
>>x=c(sum=77, min=4,max=9, count=5, min=4,max=9, count=8 ,  test=77)
>>
>>I wish to remove values where both the name and the value are identical.
>>
>>eg. i wish to end up with:
>>x2=c(sum=77, min=4,max=9, count=5, count=8, test=77)
>>
>>What is the "best" way to do this?
>>
>>
>>---------------------------------
>>Park yourself in front of a world of choices in alternative vehicles.
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>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.
>>
>>    
>>
>
>
>
>  
>



More information about the R-help mailing list