Reputation: 2489
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
Upvotes: 4
Views: 3465
Reputation: 1195
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(...)
.
Upvotes: 0
Reputation: 887088
We can use fread
which would be very fast
library(data.table)
fread(txt)
# var1 var2
#1: a 1
#2: b 2
txt = "var1,var2
a,1
b,2"
Upvotes: 3
Reputation: 37641
Try this
CSV_Text = "var1,var2
'a',1
'b',2"
Dat = read.csv(text=CSV_Text, header=TRUE)
Upvotes: 8