new ruser
2007-Jun-15 18:19 UTC
[R] removing values from a vector, where both the value and its name are the same?
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]]
jim holtman
2007-Jun-15 18:47 UTC
[R] removing values from a vector, where both the value and its name are the same?
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@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@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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Marc Schwartz
2007-Jun-15 18:49 UTC
[R] removing values from a vector, where both the value and its name are the same?
On Fri, 2007-06-15 at 11:19 -0700, new ruser 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?Not sure if this is the best way, but since you need to compare both the values and their name attributes for 'uniqueness':> x[!(duplicated(x) & duplicated(names(x)))]sum min max count count test 77 4 9 5 8 77 What is being done is to first compare the values for duplicates. Note that the second value is identified as the duplicate, not the first:> duplicated(x)[1] FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE and then compare the names for duplicates:> duplicated(names(x))[1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE Then compare the two logical vectors and get the indices where BOTH are TRUE:> (duplicated(x) & duplicated(names(x)))[1] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE Now, negate the relation:> !(duplicated(x) & duplicated(names(x)))[1] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE and return the values in 'x' that satisfy the logical indices:> x[!(duplicated(x) & duplicated(names(x)))]sum min max count count test 77 4 9 5 8 77 See ?duplicated. You might also want to look at ?unique, ?identical, ?all.equal and ?isTRUE. Note that the above example will likely fail if any of the values are floats:> duplicated(c(0.5 - 0.4, 0.1))[1] FALSE FALSE in which case, you would need to use a looping structure where the value comparisons use isTrue(all.equal(...)) instead. HTH, Marc Schwartz
Patrick Burns
2007-Jun-15 19:25 UTC
[R] removing values from a vector, where both the value and its name are the same?
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. >> >> >> > > > > >
Peter Dalgaard
2007-Jun-15 19:41 UTC
[R] removing values from a vector, where both the value and its name are the same?
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.
Marc Schwartz
2007-Jun-15 19:43 UTC
[R] removing values from a vector, where both the value and its name are the same?
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.