보다, 여러 문자열의 grepl 경기에서 일치하는 문자열을 반환 :예를 들어, 현재 내가 데이터 프레임 문자열의 벡터에 일치를 확인하기 위해 grepl와 중첩 ifelse 기능을 사용하고 오히려 논리적
# vector of possible words to match
x <- c("Action", "Adventure", "Animation")
# data
my_text <- c("This one has Animation.", "This has none.", "Here is Adventure.")
my_text <- as.data.frame(my_text)
my_text$new_column <- ifelse (
grepl("Action", my_text$my_text) == TRUE,
"Action",
ifelse (
grepl("Adventure", my_text$my_text) == TRUE,
"Adventure",
ifelse (
grepl("Animation", my_text$my_text) == TRUE,
"Animation", NA)))
> my_text$new_column
[1] "Animation" NA "Adventure"
몇 가지 요소 (예 : 여기의 3 개)에 대해서는 문제가 없지만 가능한 일치 항목이 훨씬 더 큰 경우 (예 : 150) 어떻게 반환합니까? 중첩 된 ifelse 미친 것 같습니다. 아래 코드와 같이 한 번에 여러 항목을 grepl 할 수 있다는 것을 알고 있지만이 문자열은 일치하는 문자열이 아닌 일치하는 문자열 만 반환한다는 논리를 반환합니다. 나는 경기 중 하나가 괜찮 다수의 경우 (일치 있었는지 알고 싶습니다.
x <- c("Action", "Adventure", "Animation")
my_text <- c("This one has Animation.", "This has none.", "Here is Adventure.")
grepl(paste(x, collapse = "|"), my_text)
returns: [1] TRUE FALSE TRUE
what i'd like it to return: "Animation" ""(or FALSE) "Adventure"