The read.table and read.csv functions in R are used to parse a file or URL containing delimited data and produce an R data frame. However, I already have a character vector that contains the CSV delimited data (using comma and \n as column and record delimiters), so I don't need to read it from a file or URL. How can I pass this character vector into read.table
, read.csv
, or scan()
without writing it to a file on disk first and reading it back in? I realize that writing it to disk is possible, but I am looking for a solution that does not require this needless roundtrip and can read data from the character vector directly.
Asked
Active
Viewed 8,219 times
17

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

MattJ
- 173
- 1
- 4
2 Answers
28
You can use textConnection() to pass the character vector to read.table(). An example:
x <- "first,second\nthird,fourth\n"
x1 <- read.table(textConnection(x), sep = ",")
# x1
V1 V2
1 first second
2 third fourth
Answer found in the R mailing list.
2017 EDIT
Seven years later, I'd probably do it like this:
read.table(text = x, sep = ",")

neilfws
- 32,751
- 5
- 50
- 63
-
One warning about this approach: `textConnection()` can be very slow as the number of rows increases. At 223k rows, I'm finding it faster to write to a temporary CSV, and read that in. :( – Matt Parker Oct 26 '16 at 16:10
3
A minor addendum to neilfws's answer. This wrapper function is great for helping answer questions on stackoverflow when the questioner has placed raw data in their question rather than providing a data frame.
textToTable <- function(text, ...)
{
dfr <- read.table(tc <- textConnection(text), ...)
close(tc)
dfr
}
With usage, e.g.
textToTable("first,second\nthird,fourth\n", sep = ",")

Richie Cotton
- 118,240
- 47
- 247
- 360