7

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.

ejn
  • 415
  • 6
  • 16
  • 2
    Use `dput` function. And please read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –  Feb 16 '16 at 03:33
  • If the volume of data is what matters, maybe filling a data frame at random is a solution too. For reproduciblity, you can always `set.seed(42)`. – Timothée Poisot Feb 16 '16 at 03:42

2 Answers2

12
dput(dataframeName)

in your case: dput(dat)

CuriousBeing
  • 1,592
  • 14
  • 34
5

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?

Community
  • 1
  • 1