Unique instances of a string in a vector -- there must be a better way to do this

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unique instances of a string in a vector -- there must be a better way to do this

z2.0
I'm sure there's a better way to do this using plyr. I just can't nail the right series of commands.

I've got a vector of strings. I want to remove all where the string's count within the vector is > 1.

Right now I'm using:

   
 x2$linkCount <- sapply(x2[,3], function(x) appearances = nrow(x2[x2[,3] == x,]))

Where x2 is the original data.frame and my character strings live in x2[,3]. Based on my results, that looks like it works, though if someone looks at the code and raises an eyebrow, please feel free to say why.

R 2.14.2. Windows 7, 64-bit.

Thanks,

Zack
Reply | Threaded
Open this post in threaded view
|

Re: Unique instances of a string in a vector -- there must be a better way to do this

Michael Weylandt
It's a bit of a hack, but I think you can try something like this:

x <- c(1,2,3,4,5,2,5)

duplicated(x) | duplicated(x, fromLast=T)

Michael

On Tue, Mar 27, 2012 at 7:02 PM, z2.0 <[hidden email]> wrote:

> I'm sure there's a better way to do this using plyr. I just can't nail the
> right series of commands.
>
> I've got a vector of strings. I want to remove all where the string's count
> within the vector is > 1.
>
> Right now I'm using:
>
>
>
> Where x2 is the original data.frame and my character strings live in x2[,3].
> Based on my results, that looks like it works, though if someone looks at
> the code and raises an eyebrow, please feel free to say why.
>
> R 2.14.2. Windows 7, 64-bit.
>
> Thanks,
>
> Zack
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Unique-instances-of-a-string-in-a-vector-there-must-be-a-better-way-to-do-this-tp4510489p4510489.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.