在与同事讨论绩效、教学、发送错误报告或搜索邮件列表和Stack Overflow上的指导时,经常会询问一个可重复的示例,并且总是很有用。

你有什么建议来创建一个优秀的例子?如何以文本格式粘贴r中的数据结构?您还应包括哪些其他信息?

除了使用dput()、dump()或structure()之外,还有其他技巧吗?什么时候应该包含library()或require()语句?除了c、df、data等之外,应该避免哪些保留字。?

如何做出一个伟大的、可重复的例子?


当前回答

请不要像这样粘贴控制台输出:

If I have a matrix x as follows:
> x <- matrix(1:8, nrow=4, ncol=2,
            dimnames=list(c("A","B","C","D"), c("x","y")))
> x
  x y
A 1 5
B 2 6
C 3 7
D 4 8
>

How can I turn it into a dataframe with 8 rows, and three
columns named `row`, `col`, and `value`, which have the
dimension names as the values of `row` and `col`, like this:
> x.df
    row col value
1    A   x      1
...
(To which the answer might be:
> x.df <- reshape(data.frame(row=rownames(x), x), direction="long",
+                varying=list(colnames(x)), times=colnames(x),
+                v.names="value", timevar="col", idvar="row")
)

我们不能直接复制粘贴它。

要使问题和答案正确再现,请在发布前删除+&>,并在输出和评论中添加#,如下所示:

#If I have a matrix x as follows:
x <- matrix(1:8, nrow=4, ncol=2,
            dimnames=list(c("A","B","C","D"), c("x","y")))
x
#  x y
#A 1 5
#B 2 6
#C 3 7
#D 4 8

# How can I turn it into a dataframe with 8 rows, and three
# columns named `row`, `col`, and `value`, which have the
# dimension names as the values of `row` and `col`, like this:

#x.df
#    row col value
#1    A   x      1
#...
#To which the answer might be:

x.df <- reshape(data.frame(row=rownames(x), x), direction="long",
                varying=list(colnames(x)), times=colnames(x),
                v.names="value", timevar="col", idvar="row")

还有一件事,如果您使用了某个包中的任何函数,请提及该库。

其他回答

以下是我的一些建议:

尝试使用默认的R数据集如果您有自己的数据集,请将其包含在dput中,这样其他人可以更轻松地帮助您除非确有必要,否则不要使用install.package(),人们会理解您是否只使用require或library尽量简明扼要,有一些数据集尽量简单地描述您需要的输出问问题之前自己做上传图片很容易,所以如果你有还包括您可能遇到的任何错误

所有这些都是可复制示例的一部分。

可复制代码是获得帮助的关键。然而,许多用户可能对粘贴哪怕是一大块数据都持怀疑态度。例如,他们可能在处理敏感数据,或者在研究论文中使用收集的原始数据。

出于任何原因,我认为在公开粘贴数据之前,有一个方便的函数来“变形”我的数据会很好。SciencesPo包中的匿名化函数非常愚蠢,但对我来说,它与dput函数配合得很好。

install.packages("SciencesPo")

dt <- data.frame(
    Z = sample(LETTERS,10),
    X = sample(1:10),
    Y = sample(c("yes", "no"), 10, replace = TRUE)
)
> dt
   Z  X   Y
1  D  8  no
2  T  1 yes
3  J  7  no
4  K  6  no
5  U  2  no
6  A 10 yes
7  Y  5  no
8  M  9 yes
9  X  4 yes
10 Z  3  no

然后我将其匿名化:

> anonymize(dt)
     Z    X  Y
1   b2  2.5 c1
2   b6 -4.5 c2
3   b3  1.5 c1
4   b4  0.5 c1
5   b7 -3.5 c1
6   b1  4.5 c2
7   b9 -0.5 c1
8   b5  3.5 c2
9   b8 -1.5 c2
10 b10 -2.5 c1

在应用匿名化和dput命令之前,可能还需要对一些变量而不是整个数据进行采样。

    # Sample two variables without replacement
> anonymize(sample.df(dt,5,vars=c("Y","X")))
   Y    X
1 a1 -0.4
2 a1  0.6
3 a2 -2.4
4 a1 -1.4
5 a2  3.6

通常,您需要一些数据作为示例,但是,您不想发布确切的数据。要在已建立的库中使用一些现有的data.frame,请使用data命令导入它。

例如。,

data(mtcars)

然后解决问题

names(mtcars)
your problem demostrated on the mtcars data set

除了我发现非常有趣的上述所有答案之外,有时也很容易,正如这里所讨论的:如何制作一个最小的可重复示例来获得R的帮助

有许多方法可以创建随机向量创建一个100个数字的向量,其中R中的随机值舍入为2个小数或R中的一个随机矩阵:

mydf1<- matrix(rnorm(20),nrow=20,ncol=5)

请注意,由于维度等各种原因,有时很难共享给定的数据。然而,上述所有答案都很好,在想要制作可复制的数据示例时,思考和使用这些答案非常重要。但请注意,为了使数据与原始数据一样具有代表性(以防OP无法共享原始数据),最好在数据示例中添加一些信息(如果我们将数据称为mydf1)

class(mydf1)
# this shows the type of the data you have
dim(mydf1)
# this shows the dimension of your data

此外,应该知道可以是数据结构的数据的类型、长度和属性

#found based on the following
typeof(mydf1), what it is.
length(mydf1), how many elements it contains.
attributes(mydf1), additional arbitrary metadata.

#If you cannot share your original data, you can str it and give an idea about the structure of your data
head(str(mydf1))

受到这篇文章的启发,我现在使用了一个方便的功能,当我需要发布到堆栈溢出时,repeat(<mydata>)。


快速说明

如果myData是要复制的对象的名称,请在R中运行以下命令:

install.packages("devtools")
library(devtools)
source_url("https://raw.github.com/rsaporta/pubR/gitbranch/reproduce.R")

reproduce(myData)

细节:

此函数是dput的智能包装器,执行以下操作:

自动对大型数据集进行采样(基于大小和类别。可以调整采样大小)创建dput输出允许您指定要导出的列在前面附加objName<-。。。,这样它可以很容易地复制和粘贴,但是。。。如果在Mac上工作,输出会自动复制到剪贴板,这样您就可以简单地运行它,然后将其粘贴到问题中。

可在以下位置获得来源:

GitHub-pubR/repeat.R


例子:

# sample data
DF <- data.frame(id=rep(LETTERS, each=4)[1:100], replicate(100, sample(1001, 100)), Class=sample(c("Yes", "No"), 100, TRUE))

DF约为100 x 102。我想对10行和一些特定列进行采样

reproduce(DF, cols=c("id", "X1", "X73", "Class"))  # I could also specify the column number.

提供以下输出:

This is what the sample looks like:

    id  X1 X73 Class
1    A 266 960   Yes
2    A 373 315    No            Notice the selection split
3    A 573 208    No           (which can be turned off)
4    A 907 850   Yes
5    B 202  46   Yes
6    B 895 969   Yes   <~~~ 70 % of selection is from the top rows
7    B 940 928    No
98   Y 371 171   Yes
99   Y 733 364   Yes   <~~~ 30 % of selection is from the bottom rows.
100  Y 546 641    No


    ==X==============================================================X==
         Copy+Paste this part. (If on a Mac, it is already copied!)
    ==X==============================================================X==

 DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L, 25L, 25L), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"), class = "factor"), X1 = c(266L, 373L, 573L, 907L, 202L, 895L, 940L, 371L, 733L, 546L), X73 = c(960L, 315L, 208L, 850L, 46L, 969L, 928L, 171L, 364L, 641L), Class = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label = c("No", "Yes"), class = "factor")), .Names = c("id", "X1", "X73", "Class"), class = "data.frame", row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))

    ==X==============================================================X==

还要注意,整个输出都是一个漂亮的单行,而不是一段高高的分段。这使得在Stack Overflow问题帖子上更容易阅读,也更容易复制和粘贴。


2013年10月更新:

现在,您可以指定将占用多少行文本输出(即,将粘贴到堆栈溢出中的内容)。为此,请使用lines.out=n参数。例子:

复制(DF,列=c(1:3,17,23),行.out=7)得到:

    ==X==============================================================X==
         Copy+Paste this part. (If on a Mac, it is already copied!)
    ==X==============================================================X==

 DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L,25L, 25L), .Label
      = c("A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y"), class = "factor"),
      X1 = c(809L, 81L, 862L,747L, 224L, 721L, 310L, 53L, 853L, 642L),
      X2 = c(926L, 409L,825L, 702L, 803L, 63L, 319L, 941L, 598L, 830L),
      X16 = c(447L,164L, 8L, 775L, 471L, 196L, 30L, 420L, 47L, 327L),
      X22 = c(335L,164L, 503L, 407L, 662L, 139L, 111L, 721L, 340L, 178L)), .Names = c("id","X1",
      "X2", "X16", "X22"), class = "data.frame", row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))

    ==X==============================================================X==