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', ...),....
                  )"
3
  • 3
    Use dput(mtcars)
    – Roland
    Apr 22, 2020 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, 2020 at 10:46
  • From this answer I can see that there is a package called datapasta stackoverflow.com/questions/18746456/simplified-dput-in-r which seems to be easier than dput.
    – Giacomo
    Apr 22, 2020 at 11:01

1 Answer 1

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

4
  • If you provide a complete answer I accept
    – Giacomo
    Apr 22, 2020 at 10:49
  • i updated my answer. Apr 22, 2020 at 15:22
  • Following your example I'm not clear how I can copy and paste the command for someone else to recreate the dataframe in their own environment.
    – Giacomo
    Apr 22, 2020 at 15:28
  • 1. run the dput () function on your data.frame, 2. copy the output from the console. Apr 22, 2020 at 15:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.