Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R read.csv data from inline string with a csv file content

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

like image 245
LucasMation Avatar asked Dec 24 '22 22:12

LucasMation


1 Answers

Try this

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

Dat = read.csv(text=CSV_Text, header=TRUE)
like image 165
G5W Avatar answered Feb 24 '23 10:02

G5W