0

Is there a way in R to take a dataframe and auto-generate lines of code that would, if run, create that same dataframe from scratch?

So if my starting point is a dataframe df:

field_1   field_2 
————————————————-
1         A
2         B
3         C

I’d like some way to generate code automatically to recreate the data, eg:

df <- data.frame(field_1 = c(1, 2, 3), field_2 = c(“A”, “B”, “C”))

In my case these dataframes would always be small - max about 50 rows and 3-4 columns.

Thank you.

Alan
  • 619
  • 6
  • 19
  • 6
    The `dput()` function does exactly that. – Dirk Eddelbuettel Apr 07 '20 at 01:39
  • Careful with your quotes... looks like you pasted your code into an editor that autocorrects plain quotes `"` with fancy quotes `“` and `”`. This will make your code un-runnable. – Gregor Thomas Apr 07 '20 at 01:50
  • 1
    Along with the builtin `dput` function, there's the `datapasta` package for copying a table off a document or webpage and pasting it directly as the R code to make the dataframe. – Brian Apr 07 '20 at 01:58
  • @GregorThomas ah yes, I hadn’t noticed that. I posted this using an iPad, which I don’t normally do, so I guess that’s why – Alan Apr 07 '20 at 02:23
  • @Brian Thank you. I’ve just seen a quick demo of this on YouTube and I think it could be very useful for me. – Alan Apr 07 '20 at 02:24

0 Answers0