Read text inline in R

Sometimes it is convenient to have the data in the same file as the computation in R. Further complications arise that text files often have irregular number of columns or empty columns. Reading such data with read.table() into R is accomplished in the following way:

r = read.table(sep=’\n’, stringsAsFactors = FALSE,
text = “ORDER DATE
STATUS
SOURCE
DELIVERY MODE
ORDER NUMBER
AMOUNT
06/22/2017″)
# get all the dates
grep(‘^\\d{2}’,r$V1)
The text can then be parsed separately and saved in a data.frame() object. 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.