[R] Print occurrence / positions of words

S Ellison S.Ellison at LGCGroup.com
Fri Apr 26 16:33:04 CEST 2013

> >I have tried some different packages in order to build a R program
> which will take as input a text file, produce a list of the 
> words inside  that file. Each word should have a vector with 
> all the places that this  word exist in the file. 

How about

txt <- paste(rep("this is a nice text with nice characters", 3), "But this is not", collapse=" ")

txt.vec <-str_split(txt, "[^[:alnum:]_]+")[[1]] 
	#vector of all the words in their original sequence

tapply(1:length(txt.vec), txt.vec, c)
	#Returns a list of vectors of locations of each word, sorted alphabetically

S Ellison

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list