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

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

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

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


当前回答

以下是我的一些建议:

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

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

其他回答

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

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")

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

这是一个很好的指南。

最重要的一点是:制作一小段代码,我们可以运行它来了解问题所在。一个有用的函数是dput(),但是如果您有非常大的数据,那么您可能需要制作一个小样本数据集,或者只使用前10行左右。

编辑:

此外,确保您确定了问题所在。示例不应该是一个完整的R脚本,其中包含“在第200行出现错误”。如果您使用R(我爱浏览器())和Google中的调试工具,那么您应该能够真正确定问题所在,并重现一个同样错误的小例子。

到目前为止,对于再现性部分,答案显然很好。这只是为了澄清,一个可复制的例子不能也不应该是问题的唯一组成部分。别忘了解释你希望它看起来是什么样子,以及你的问题的轮廓,而不仅仅是你迄今为止试图达到的目的。代码不够;你也需要语言。

这里有一个可重复的例子来说明应该避免做什么(从一个真实的例子中得出,为了保护无辜者而改变了名字):


以下是示例数据和我遇到问题的部分函数。

code
code
code
code
code (40 or so lines of it)

我怎样才能做到这一点?


受到这篇文章的启发,我现在使用了一个方便的功能,当我需要发布到堆栈溢出时,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==

就我个人而言,我更喜欢“一”行。大致如下:

my.df <- data.frame(col1 = sample(c(1,2), 10, replace = TRUE),
        col2 = as.factor(sample(10)), col3 = letters[1:10],
        col4 = sample(c(TRUE, FALSE), 10, replace = TRUE))
my.list <- list(list1 = my.df, list2 = my.df[3], list3 = letters)

数据结构应该模仿作者问题的想法,而不是准确的逐字结构。当变量不覆盖我自己的变量或函数(如df)时,我真的很感激。

或者,你可以切几个角,指向一个预先存在的数据集,比如:

library(vegan)
data(varespec)
ord <- metaMDS(varespec)

不要忘记提及您可能使用的任何特殊软件包。

如果你想在更大的物体上演示一些东西,你可以尝试

my.df2 <- data.frame(a = sample(10e6), b = sample(letters, 10e6, replace = TRUE))

如果通过光栅包处理空间数据,则可以生成一些随机数据。在包装小插曲中可以找到很多例子,但这里有一个小亮点。

library(raster)
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- stack(r1, r2, r3)

如果您需要一些在sp中实现的空间对象,可以通过“空间”包中的外部文件(如ESRI shapefile)获取一些数据集(请参见任务视图中的空间视图)。

library(rgdal)
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="cities")
cities <- readOGR(dsn=dsn, layer="cities")