I have the following problem on R: I have a string such as this string = "Joe attended the university of Harvard" and a list of names of universities, e.g. list = "Harvard, MIT, Yale,...".
I want a function in R that returns all the words from the list so that, in this case, it would be f(string, list) = "Harvard"
CodePudding user response:
Like this:
Data:
str <- c("Joe attended the university of Harvard",
"Peter went to Yale",
"Sue was at Illinois State")
List of universities:
unis <- c("Harvard", "Yale", "MIT")
Extraction:
library(stringr)
str_extract(str, paste0(unis, collapse = "|"))
[1] "Harvard" "Yale" NA
