MGIGijs
MGIGijs

Reputation: 47

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

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.

input

x <- c("139H22V01",
"129H22V01",
"125H22V01",
"116H22V01",
"168H22V01",
"175H22V01",
"204H22V01",
"258H23V01",
"168H23V01",
"116H23V01",
"129H22V02",
"168H22V02")

expected output:

outp <- c( "129H22V01" , "258H23V01" , "129H22V02" )

Upvotes: 1

Views: 200

Answers (1)

Henrik
Henrik

Reputation: 67828

You may try this:

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

Upvotes: 2

Related Questions