[R] Retrieving the context

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Apr 2 13:37:20 CEST 2009


Example:

see_context <- function(word, data, context) {
     dat <- data$strings
     temp <- grep(paste("^", word, "$", sep=""), dat)
     if(length(temp))
         index <- lapply(temp, function(x) seq(max(0, x - context), 
min(x + context, length(dat))))
     sapply(index, function(x) paste(dat[x], collapse=" "))
}
see_context("is", mydf, 1)
see_context("is", mydf, 2)


Uwe Ligges


Fredrik Karlsson wrote:
> Dear list,
> 
> I have a general problem that I really don't know how to solve efficiently
> in R. Lets say we have a sequence of things, like for instance a string of
> words, that is stored in a file. We need all the words in a table format, so
> therefore we create an id for the word, that links the word to a file and
> the position of the word within the file, like:
> 
> #In this case a very short file
>> strsplit("This is a text string, wich is stored in the file myfile","
> ")[[1]] -> mystring
> #Now, store in a data.frame
>> mydf <- data.frame(strings=mystring,
> word_id=paste("myfile",1:length(mystring),sep="_"))
>> mydf
>    strings   word_id
> 1     This  myfile_1
> 2       is  myfile_2
> 3        a  myfile_3
> 4     text  myfile_4
> 5  string,  myfile_5
> 6     wich  myfile_6
> 7       is  myfile_7
> 8   stored  myfile_8
> 9       in  myfile_9
> 10     the myfile_10
> 11    file myfile_11
> 12  myfile myfile_12
> 
> Now, I would like to see all the words 'is' in a user defined window: so
> see_context("is",mydf,1) would give
> This is a
> wich is stored
> 
> and see_context("is",mydf,2) would show two words before and after.. and so
> on.
> 
> Any ideas on how to solve this kind of problem in R?
> 
> /Fredrik
> 
>



More information about the R-help mailing list