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.

2
  • you need to escape \n by doing \\n. Try it Commented Feb 18, 2016 at 0:39
  • 2
    summary is an unusual/wrong approach to investigating an object's structure. Try str or class or just typing the object's name. Commented Feb 18, 2016 at 0:57

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Do you mean strsplit(text?
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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.