Greetings, I am not familiar with processing text in R. Can someone tell me how to read each line of words as separate elements in a list? FE, I would like to turn: word1 word2 word3 word2 word4 into a list of length two with three character elements in the first list and two elements in the second. I know that this should be easy, but I am a little confused by the text functions. Thanks in advance! -- View this message in context: http://old.nabble.com/reading-tokens-tp26159915p26159915.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> x <- readLines(textConnection("word1 word2 word3+ word2 word4"))> closeAllConnections() > yourList <- strsplit(x, '[[:space:]]+') > > > yourList[[1]] [1] "word1" "word2" "word3" [[2]] [1] "word2" "word4">On Mon, Nov 2, 2009 at 8:00 PM, j daniel <jdlecy at maxwell.syr.edu> wrote:> > Greetings, > > I am not familiar with processing text in R. ?Can someone tell me how to > read each line of words as separate elements in a list? > > FE, I would like to turn: > > word1 word2 word3 > word2 word4 > > into a list of length two with three character elements in the first list > and two elements in the second. ?I know that this should be easy, but I am a > little confused by the text functions. > > Thanks in advance! > -- > View this message in context: http://old.nabble.com/reading-tokens-tp26159915p26159915.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Nov 2, 2009, at 8:00 PM, j daniel wrote:> > Greetings, > > I am not familiar with processing text in R. Can someone tell me > how to > read each line of words as separate elements in a list? > > FE, I would like to turn: > > word1 word2 word3 > word2 word4 > > into a list of length two with three character elements in the first > list > and two elements in the second. I know that this should be easy, > but I am a > little confused by the text functions.> txt <- textConnection("word1 word2 word3 + word2 word4") > strsplit(readLines(txt), " ") [[1]] [1] "word1" "word2" "word3" [[2]] [1] "word2" "word4">-- David Winsemius, MD Heritage Laboratories West Hartford, CT
j daniel wrote:> > I am not familiar with processing text in R. Can someone tell me how to > read each line of words as separate elements in a list? > > FE, I would like to turn: > > word1 word2 word3 > word2 word4 > > into a list of length two with three character elements in the first list > and two elements in the second. I know that this should be easy, but I am > a little confused by the text functions. >You could use scan. Have a look at package gsubfn, where there is a demo, that show additional features you are going to use library(gsubfn) demo(gsubfn-gries) .... The example code is a bit overnested, but to better understand what is going on, unwrap it: So tail(sort(table(unlist(strapply(Lines1, "\\w+", perl = TRUE))))) is: x1 = strapply(Lines1, "\\w+", perl = TRUE) x1 x2 = ulist(x2) x2 x3 = table(x2) x3 x4 = sort(x3) x4 tail(x4) Dieter -- View this message in context: http://old.nabble.com/reading-tokens-tp26159931p26160018.html Sent from the R help mailing list archive at Nabble.com.