[R] spliting first 10 words in a string

David Winsemius dwinsemius at comcast.net
Mon Nov 1 22:12:43 CET 2010


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



More information about the R-help mailing list