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

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Fri, 15 Jun 2007 20:25:20 +0100

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



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 - 20:40:29 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 - 22:32:22 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.