Dre
Dre

Reputation: 723

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.

Upvotes: 2

Views: 2553

Answers (2)

HubertL
HubertL

Reputation: 19544

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

Upvotes: 6

akrun
akrun

Reputation: 887981

Other option would be to scan the string to a vector directly

 summary(scan(text=text, what='', quiet=TRUE))
 #   Length     Class      Mode 
 #        5 character character 

Upvotes: 2

Related Questions