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

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

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

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


当前回答

从R.2.14开始(我猜),您可以将数据文本表示直接输入read.table:

 df <- read.table(header=TRUE, 
  text="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
") 

其他回答

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

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的帮助

有许多方法可以创建随机向量创建一个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))

您可以使用reprex执行此操作。

正如mt1022所指出的,“……生产最小、可重复示例的好包装是tidyverse的“reprex”。”。

根据Tidyverse的说法:

“reprex”的目标是以这样一种方式打包您的问题代码,使其他人可以运行它并感受到您的痛苦。

tidyverse网站上给出了一个示例。

library(reprex)
y <- 1:4
mean(y)
reprex() 

我认为这是创建可复制示例的最简单方法。

最初的帖子指的是现已退役的数据营的小提琴演奏服务。它已被重新命名为数据营灯,不能像我的回答所示的那样容易嵌入。

我想知道http://old.r-fiddle.org/链接可能是分享问题的一种非常简单的方式。它接收一个唯一的ID,比如,甚至可以考虑将其嵌入SO中。

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

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


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

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

我怎样才能做到这一点?