I have a vector of strings. Check out my vector, it's awesome
:
> awesome
[1] "a" "b" "c" "d" "d" "e" "f" "f"
I'd like to make a new vector that is the same length as awesome
but where, if necessary, the strings have been uniqueified. For example, a valid output of my desired function would be
> awesome.uniqueified
[1] "a" "b" "c" "d.1" "d.2" "e" "f.1" "f.2"
Is there an easy, R-thonic and beautiful way to do this? I should say my list in real life (it's not called awesome
) contains 25000ish mircoarray probeset identifiers.
I'm always nervous when I embark on writing little generic functions (which I'm sure I could do) as I'm sure some R guru has come across this problem in the past, nailed it with some incredible algorithm that doesn't even have to store more than half an element in the vector. I'm just not sure what they might have called it. Probably not uniqueify
.
unique(c(A, B)) == make. unique(c(make. unique(A), B)) . In other words, you can append one string at a time to a vector, making it unique each time, and get the same result as applying make.
A character vector is a set of strings stored as the single object.
Try make.unique()
where the very first example of the help page is already spot-on:
make.unique(c("a", "a", "a"))
[1] "a" "a.1" "a.2"
The help page lists Thomas Minka as author. Buy him a beer one day :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With