0

I have the string

She was the youngest of the two daughters of a most affectionate

and I want to turn this into a vector like below

she was the youngest etc.

I'd like to use stringr if possible.

Thank you.

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27

2 Answers2

2

Any of the following could work:

scan(text=charv, what = character())
 [1] "She"          "was"          "the"          "youngest"     "of"           "the"         
 [7] "two"          "daughters"    "of"           "a"            "most"         "affectionate"

or

unlist(strsplit(charv,' '))

 [1] "She"          "was"          "the"          "youngest"     "of"           "the"         
 [7] "two"          "daughters"    "of"           "a"            "most"         "affectionate"

or

read.table(text=gsub(' ','\n',charv))
             V1
1           She
2           was
3           the
4      youngest
5            of
6           the
7           two
8     daughters
9            of
10            a
11         most
12 affectionate

or

 unlist(regmatches(charv,gregexpr('\\w+',charv)))
 [1] "She"          "was"          "the"          "youngest"     "of"           "the"         
 [7] "two"          "daughters"    "of"           "a"            "most"         "affectionate"

Where:

 charv<-'She was the youngest of the two daughters of a most affectionate'

EDIT: using stringr: Any of the following

library(stringr)
str_extract_all(charv, '\\w+')
str_split(charv," ")
Onyambu
  • 67,392
  • 3
  • 24
  • 53
1

Try this:

charv <- 'She was the youngest of the two daughters of a most affectionate'

#Code
x <- do.call(c,strsplit(charv,split = ' '))

[1] "She"          "was"          "the"          "youngest"     "of"           "the"          "two"         
[8] "daughters"    "of"           "a"            "most"         "affectionate"
Duck
  • 39,058
  • 13
  • 42
  • 84