2

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.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
Dre
  • 713
  • 1
  • 8
  • 27

2 Answers2

6

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
HubertL
  • 19,246
  • 3
  • 32
  • 51
2

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 
akrun
  • 874,273
  • 37
  • 540
  • 662