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

Marc Schwartz marc_schwartz at comcast.net
Fri Jun 15 21:43:53 CEST 2007


Good catch Patrick.  I am guilty of the same error.

Regards,

Marc

On Fri, 2007-06-15 at 20:25 +0100, 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).
> 
> 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.



More information about the R-help mailing list