0

is there a way in R to produce the "code" to create a dataframe, based on an existing dataframe? This would make it easier to ask SO questions with reproducible examples.

For example:

> data(mtcars)
> df <- head(mtcars)
> df
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

magic_function(df)

"df <- data.frame(mpg  = c('value_1', 'value_2', ...),
                  cyl = c('value_1', 'value_2', ...),....
                  )"
Giacomo
  • 1,796
  • 1
  • 24
  • 35
  • 3
    Use dput(mtcars) – Roland Apr 22 '20 at 10:37
  • yes it does. Thank you all. To be honest i tried to look for this online for a bit and couldn't find it, so maybe it's worth keeping this if I put a complete and clean answer below. Not sure though, I don't want to duplicate unnecessarily. – Giacomo Apr 22 '20 at 10:46
  • From this answer I can see that there is a package called `datapasta` https://stackoverflow.com/questions/18746456/simplified-dput-in-r which seems to be easier than dput. – Giacomo Apr 22 '20 at 11:01

1 Answers1

2

the function you are looking for is "dput(mtcars)" as Roland pointed out in his comment.

Example:

df1<-data.frame(ID= 1:10, y = rnorm(10, 50), group= rep(c("A","B"),5))

#  ID        y group
#1   1 49.91448     A
#2   2 50.71076     B
#3   3 51.99664     A
#4   4 50.89964     B
#5   5 49.92638     A
#6   6 49.68232     B
#7   7 48.69369     A
#8   8 50.15223     B
#9   9 49.14595     A
#10 10 51.19469     B


> dput(df1)
** Copy following output from console**

# structure(list(ID = 1:10, y = c(49.5838950074112, 48.2157755931339, 
#49.6788263608271, 49.2647058738269, 50.8090753588302, 51.5603249320185, 
#49.1274987940471, 50.3875737336775, 50.067929898497, 49.5100624688757
#), group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
#), .Label = c("A", "B"), class = "factor")), class = "data.frame", row.names = c(NA, 
#-10L))

** paste it back and assign to df2**

df2 <- structure(list(ID = 1:10, y = c(49.5838950074112, 48.2157755931339, 
49.6788263608271, 49.2647058738269, 50.8090753588302, 51.5603249320185, 
49.1274987940471, 50.3875737336775, 50.067929898497, 49.5100624688757),
group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L),
 .Label = c("A", "B"), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

> all.equal(df1,df2)
[1] TRUE

user12256545
  • 2,755
  • 4
  • 14
  • 28