jamse
jamse

Reputation: 354

Find which element of one character vector another vector's elements start with

I have two character vectors:

v1 <- c("Red 1", "Red 2", "Red 3", "Blue thing", "Blue car", "Yellow anything")
v2 <- c("Yellow", "Red", "Blue")

I would like to get a vector, the length of v1, containing the index in v2 of the element that it starts with.

# Desired result
result <- c(2, 2, 2, 3, 3, 1)

I have tried looking at some combination of which() and startsWith() but have not been able to vectorise the operation over both vectors.

Upvotes: 1

Views: 579

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101753

Maybe we can try gsub + match like below

> match(gsub(sprintf("[^(%s)]", paste0(v2, collapse = "|")), "", v1), v2)
[1] 2 2 2 3 3 1

or

> match(gsub("\\s.*", "", v1), v2)
[1] 2 2 2 3 3 1

Upvotes: 2

akrun
akrun

Reputation: 887223

We could use outer for efficiency

which(t(outer(v1, v2, FUN = function(x, y) startsWith(x, y))), arr.ind = TRUE)[,1]
[1] 2 2 2 3 3 1

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389047

Using sapply -

as.numeric(sapply(v1,function(x) which(sapply(v2,function(y) startsWith(x, y)))))

#[1] 2 2 2 3 3 1

Upvotes: 3

Related Questions