On Nov 1, 2010, at 4:39 PM, Matevž Pavlič wrote:

Hi all,



I have a columnn with text that has quite a few words in it. I would like to split these words in separate columns, but just first ten words in the string. Is that possible in R?



Not sure what a column means to you. It's not a precisely defined R type or class. (And you are requested to offered a concrete example rather than making us guess.)

>words <-"I have a columnn with text that has quite a few words in it. I would like to split these words in separate columns, but just first ten words in the string. Is that possible in R?"

> strsplit(words, " ")[[1]][1:10]
[1] "I" "have" "a" "columnn" "with" "text" "that" "has" "quite" "a"


Or if in a dataframe:

> words <-c("I have a columnn with text that has quite a few words in it.", "I would like to split these words in separate columns", "but just first ten words in the string. Is that possible in R?")
> worddf <- data.frame(words=words)

> t(sapply(strsplit(worddf$words, " "), "[", 1:10) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [, 8] [,9] [,10] [1,] "I" "have" "a" "columnn" "with" "text" "that" "has" "quite" "a" [2,] "I" "would" "like" "to" "split" "these" "words" "in" "separate" "columns" [3,] "but" "just" "first" "ten" "words" "in" "the" "string." "Is" "that"


--
David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to