r - substring + get words around a keyword -
if have string:
moon <- "the cow jumped on moon silver plate in mouth"
is there way can extract words in neighborhood of "moon"
. neighborhood 2 or 3 words around "moon".
so if
"the cow jumped on moon silver plate in mouth"
i want output be:
"jumped on moon silver"
i know can use str_locate
if wanted extract characters, not sure how using "words". can done in r?
thanks & regards, simak
use strsplit
:
x <- strsplit(str, " ")[[1]] <- which(x == "moon") paste(x[seq(max(1, (i-2)), min((i+2), length(x)))], collapse= " ")
Comments
Post a Comment