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

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -