35

Being a new user here, my questions are not being fully answered due to not being reproducible. I read the thread relating to producing reproducible code but to avail. Specifically lost on how to use the dput() function.

Could someone provide a step by step on how to use the dput() using the iris df for eg it would be very helpful.

user438383
  • 5,716
  • 8
  • 28
  • 43
Tyler
  • 543
  • 4
  • 13
  • 3
    If your dataframe is `df` you just need to do `dput(df)` or `dput(iris)` in your case and copy the output and paste it here and I believe you meant [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) . – Ronak Shah Apr 24 '18 at 05:50
  • 4
    I suggest you [read it again](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It do have some examples of `dput`. – mt1022 Apr 24 '18 at 05:52
  • 6
    And if the data frame is large, but the first few rows are representative enough to illustrate your question, you can do _e.g._ `dput(head(iris, 10))` (first 10 rows). – neilfws Apr 24 '18 at 05:53
  • yes my df is masssize, with 100+ columns. How do i select specific columns also.. as in column 86 and column 88 and column 99 for example? Thanks! – Tyler Apr 24 '18 at 05:57
  • subset the dataframe and then use `dput` i.e `dput(df[1:10, c(86, 88, 99)])`. – Ronak Shah Apr 24 '18 at 05:59
  • Like so? Subsetted the df to only one varaible which is the one I want to create a pie chart with dput(head(stack,1, 10)) structure(list(RatingStatus = "RATED"), .Names = "RatingStatus", row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")) – Tyler Apr 24 '18 at 07:12

1 Answers1

30

Using the iris dataset, which is handily included into R, we can see how dput() works:

data(iris)
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Now we can get the whole dataset using dput(iris). In most situations, a whole dataset is unnecessary to provide for a Stackoverflow question, as a few lines of the relevant variables suffice as a working data example.

Two things come in handy: The head() function outputs only the first six rows of a dataframe/matrix. Also, the indexing in R (via brackets) allows you to select only specific columns.

Therefore, we can restrict the output of dput() using a combination of these two:

dput(head(iris[, c(1, 3)]))

structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), 
    Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7)), .Names = c("Sepal.Length", 
"Petal.Length"), row.names = c(NA, 6L), class = "data.frame")

will give us the code to reproduce the first (up to) six rows of column 1 and 3 of the iris dataset.

df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), 
    Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7)), .Names = c("Sepal.Length", 
"Petal.Length"), row.names = c(NA, 6L), class = "data.frame")

> df
  Sepal.Length Petal.Length
1          5.1          1.4
2          4.9          1.4
3          4.7          1.3
4          4.6          1.5
5          5.0          1.4
6          5.4          1.7

If the first rows do not suffice, we can skip using head() and rely on indexing only:

dput(iris[1:20, c(1, 3)])

structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1
), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 
1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5)), .Names = c("Sepal.Length", 
"Petal.Length"), row.names = c(NA, 20L), class = "data.frame")

will give us the the first twenty rows:

df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1
), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 
1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5)), .Names = c("Sepal.Length", 
"Petal.Length"), row.names = c(NA, 20L), class = "data.frame")

> df
   Sepal.Length Petal.Length
1           5.1          1.4
2           4.9          1.4
3           4.7          1.3
4           4.6          1.5
5           5.0          1.4
6           5.4          1.7
7           4.6          1.4
8           5.0          1.5
9           4.4          1.4
10          4.9          1.5
11          5.4          1.5
12          4.8          1.6
13          4.8          1.4
14          4.3          1.1
15          5.8          1.2
16          5.7          1.5
17          5.4          1.3
18          5.1          1.4
19          5.7          1.7
20          5.1          1.5
LAP
  • 6,605
  • 2
  • 15
  • 28
  • 4
    I am still not sure, what to do next if I want to post my MWE in stackoverflow. Should just copy and paste what is in the 'structure()' – msh855 Jul 14 '19 at 16:41
  • 2
    @msh855 Yes, including the `structure()`. – LAP Jul 23 '19 at 08:46
  • 1
    @msh855 I know this is older but I'm commenting to help others. One thing you should always do is take the code you want to post and trying running it in a clean session of R. You'll get a better idea of what you're providing the person who is trying to help you. – Dason May 11 '22 at 12:43