0

I have 2 character vectors:

Long <- c("d4@NHL1", "d7@NHL2", "d4@NHL3", "d7@NHL4", "d7@NHL5", "d9@NHL6")
Short <- c("NHL1", "NHL3", "NHL6", "NHL5")

I need to check which strings from Long vector partly match strings from Short vector, starting from @ symbol. I need to get a logic vector as a result.

I think I need to use grepl() function, something like this:

sapply(Long, function(x) grepl(x, paste(Short, collapse = "")))

I don't know what to use as a pattern in grepl() function or if there is any argument to start matching after a symbol.

Maryia Maziuk
  • 33
  • 1
  • 7

1 Answers1

2

Borrowing the approach from another stackoverflow response:

Long <- c("d4@NHL1", "d7@NHL2", "d4@NHL3","d7@NHL4", "d7@NHL5", "d9@NHL6")
Short <- c("NHL1", "NHL3", "NHL6", "NHL5")
matches <- grep(paste0("*@",Short,collapse="|"), 
                Long, value=TRUE)

The * matches any number of characters before the @

Community
  • 1
  • 1
raninjan
  • 186
  • 7