I couldn't think of exactly what the question should have been so if you have a suggestion for what it should be please let me know.
I've seen before a way to read data into a data frame that is tabbed or white spaced in your working script file. For example:
dat <- SOMETHING(
person1 12 15
person2 15 18
person3 20 14
)
Say you're grabbing data from a website and just want to table a few things, and it comes off like this with white space etc. I could open a text file and save it and then read.table or similar with csv but I'm sure I've seen data read in this way and can't for the life of me remember how...
Thanks
The "trick" is a text connection as the "file" argument to read.table:
dat <- read.table(textConnection("person1 12 15
person2 15 18
person3 20 14"), stringsAsFactors=FALSE
)
str(dat)
'data.frame': 3 obs. of 3 variables:
$ V1: chr "person1" "person2" "person3"
$ V2: int 12 15 20
$ V3: int 15 18 14
The default 'sep' argument works for whitespace separation. If you need tabs to separate then use sep="\t" (after the closing-paren from the textConnection
call).
Edit: This actually got incorporated into a subsequent revision of the underlying scan
function which was given a 'text'-argument. The code could now simply be:
dat <- read.table(text="person1 12 15
person2 15 18
person3 20 14", stringsAsFactors=FALSE
)
The readLines
function still requires the use of textConnection
to read from a 'character'-object, since it does not use scan
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With