ejn
ejn

Reputation: 463

How to export R data frame into code for sharing?

I'm trying to write a question for Stack Overflow, and I need to include a data frame so that my data can be replicated. I would like to take a data.frame object, and output code. I.e., I want to take this data.frame:

> dat
  a b
1 1 4
2 2 5
3 3 6

And turn it into this code without having to write it all out myself:

> dat <- data.frame(a=c(1,2,3),b=c(4,5,6))

My data.frame is very large, and I need to have a large number of observations for my question to make sense. Therefore it would be great if there is a package that will write that code out for me.

Thanks a lot for any and all advice.

Upvotes: 12

Views: 2061

Answers (2)

CuriousBeing
CuriousBeing

Reputation: 1632

dput(dataframeName)

in your case: dput(dat)

Upvotes: 15

user3710546
user3710546

Reputation:

Please use dput function:

dput(dat)

If your data.frame is huge, you need to sample it.

And first, if not yet done, please read How to make a great R reproducible example?

Upvotes: 6

Related Questions