How to find the first unique encounters of a sub string in R

Tag: r , select , substring , unique Author: granger1221 Date: 2014-02-09

Could you please help me with the following R scripting problem:

I would like to select and save the strings which have the first unique value of a substring. In this case I would like to know the first strings that have a unique combination of substring H..V.. substring 4 to 9

I would like to find all the unique H and V patterns, such as H22V01, next unique would be H23V01 etc, and then return the first full string per unique substring that belongs to these unique substrings.


x <- c("139H22V01",

expected output:

outp <- c( "129H22V01" , "258H23V01" , "129H22V02" )
In your examples there are no unique H..V.. patterns ?
I have edited my question, so hopefully it would be more clear. Thanks
If I understand right, you need first occurence of each H..V.. ? You should also include expected output from the provided input, and how do you intend to accomplish this, what language, scripts, etc.?
yep, thanks a lot, bit new to this.
Can you please explain why you have "129H22V01" and not "139H22V01" in your expected output?

Best Answer

You may try this:

x[!duplicated(substring(x, first = 4))]
# [1] "139H22V01" "258H23V01" "129H22V02"


works! precisely what I needed