Dre Dre -4 years ago 113
R Question

How to make a vector of character strings using strsplit in R?

I have a block of text that I have scraped into R and is being read as one long character string.

Example of block of text:

[1] "abc \n 18:19 \n abc \n 7-9 \n abc \n"


Summary of block of text:

summary(text)
Length Class Mode
1 character character


I then do a strsplit
text <- strsplit(text, "\n")


Summary of text after
strsplit


summary(text)
Length Class Mode
[1,] 5 -none- character


What I would like when I complete the
strsplit


summary(text)
Length Class Mode
5 character character


Any help will be appreciated. Please let me know if anymore information is needed.

Answer Source

The result of strsplit() is a list() of character:

mytext <- "abc \n 18:19 \n abc \n 7-9 \n abc \n"
text <- strsplit(mytext, "\n")
class(text)
[1] "list"

Each element of the list is of class character

summary(text[[1]])
Length     Class      Mode 
     5 character character 

To convert the list to a vector you can unlist() it:

text=unlist(text)
summary(text)
   Length     Class      Mode 
        5 character character
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download