4

this is supposed to be a trivial thing, but I have not found anything googling it.

I have the following data in a csv file

test.csv

var1,var2
'a',1
'b',2

which I read into R with

d <- read.csv('test.csv')

This there a way for me to insert the content of the csv file in my R code? Something like:

d <- read.csv(
    'var1,var2\n
    'a',1\n
    'b',2')

(above syntax does not work)

how can I tell R that the input string should be treated as the data itself instead of a file name containing it?

This would be for adding small tables to Rmarkdown without having to create a bunch of auxiliary files

of course I could add the same info with

d <- data.frame(
  var1=c('a','b'),
  var2=1,2
)

but listing the data vector by vector slow the process down. The row by row structure of the csv is easyer

LucasMation
  • 2,408
  • 2
  • 22
  • 45

3 Answers3

8

Try this

CSV_Text = "var1,var2
'a',1
'b',2"

Dat = read.csv(text=CSV_Text, header=TRUE)
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thanks for this! :) The key here is the `text` argument to `read.table()` (and, by extension, `read.csv()`). I missed it when I was looking through the help page because `read.table()` has sooo many arguments, and `text` is way down near the end of the list! `text: character string: if ‘file’ is not supplied and this is, then data are read from the value of ‘text’ via a text connection. Notice that a literal string can be used to include (small) data sets within R code.` – Aaron Wells Dec 05 '17 at 21:36
3

We can use fread which would be very fast

library(data.table)
fread(txt)
#   var1 var2
#1:    a    1
#2:    b    2

data

txt = "var1,var2
  a,1
  b,2"
akrun
  • 874,273
  • 37
  • 540
  • 662
0

The tibble package's tribble() method, is another great option for writing short bits of tabular data in an R file, in a way that is human-readable, machine-readable, and easy to maintain/edit. And as a bonus, it makes it easy to use R expressions as values in the table.

# Column names are prefaced with "~"
my_tibble1 <- tribble(
  ~colA, ~colB, ~colC,
  "a",   1,     10.1,
  "b",   2,     3.4,
  "c",   3,     5.6
)
#> # A tibble: 3 x 3
#>    colA  colB  colC
#>   <chr> <dbl> <dbl>
#> 1     a     1  10.1
#> 2     b     2   3.4
#> 3     c     3   5.6


# tribble will create a list column if the value in any cell is
# not a scalar
my_tibble2 <- tribble(
  ~x,  ~y,
  "a", 1:3,
  "b", 4:6
)
#> # A tibble: 2 x 2
#>       x         y
#>   <chr>    <list>
#> 1     a <int [3]>
#> 2     b <int [3]>

If you haven't encountered tibbles before, they're a (mostly) drop-in replacement for data.frame. If you need to really make sure that your data is a data.frame and not a tibble, you can convert it to one with as.data.frame(...).

Aaron Wells
  • 1,135
  • 7
  • 5