Dario Lacan
Dario Lacan

Reputation: 1140

vectorize stringr str_match to remove a for loop

how can I use R vectorization to remove this for loop?

library(stringr)
url <- c("http://www.slate.fr/france/87869/mehdi-nemmouche-bruxelles", "http://www.slate.fr/story/87347/turquie-opposition-geek", "http://www.slate.fr/grand-format/paysages-debarquement-photos-1944-aujourdhui")

for (i in 1:length(url)) {
a[i]<-str_match(url[i], "http://.*slate.fr/(.*?)/")[2]
}

This does not work:

a<-str_match(url, "http://.*slate.fr/(.*?)/")[2]

Upvotes: 2

Views: 360

Answers (1)

akrun
akrun

Reputation: 887118

You have to use [,2] instead of [2] because the output is a 2 column matrix and by indexing [2], you are getting only the 2nd element i.e. "http://www.slate.fr/story/" instead of the 2nd column`.

str_match(url, "http://.*slate.fr/(.*?)/")[,2]
#[1] "france"       "story"        "grand-format"

From the description of ?str_match

Vectorised over ‘string’. ‘pattern’ should be a single pattern, i.e. a character vector of length one.

Upvotes: 3

Related Questions