在与同事讨论绩效、教学、发送错误报告或搜索邮件列表和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.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
") 

如果您有一个大数据集,无法使用dput()轻松放入脚本,请将数据发布到pastebin并使用read.table加载它们:

d <- read.table("http://pastebin.com/raw.php?i=m1ZJuKLH")

灵感来自Henrik。

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

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

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

我正在开发wakefield包,以解决快速共享可复制数据的需求,有时dput对较小的数据集很好,但我们处理的许多问题要大得多,通过dput共享如此大的数据集是不切实际的。

关于:

wakefield允许用户共享最少的代码来再现数据。用户设置n(行数)并指定任意数量的预设变量函数(目前有70个),这些函数模拟真实的if数据(如性别、年龄、收入等)

安装:

目前(2015年6月11日),wakefield是一个GitHub包,但在编写单元测试后,最终将转到CRAN。要快速安装,请使用:

if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/wakefield")

例子:

下面是一个示例:

r_data_frame(
    n = 500,
    id,
    race,
    age,
    sex,
    hour,
    iq,
    height,
    died
)

这将产生:

    ID  Race Age    Sex     Hour  IQ Height  Died
1  001 White  33   Male 00:00:00 104     74  TRUE
2  002 White  24   Male 00:00:00  78     69 FALSE
3  003 Asian  34 Female 00:00:00 113     66  TRUE
4  004 White  22   Male 00:00:00 124     73  TRUE
5  005 White  25 Female 00:00:00  95     72  TRUE
6  006 White  26 Female 00:00:00 104     69  TRUE
7  007 Black  30 Female 00:00:00 111     71 FALSE
8  008 Black  29 Female 00:00:00 100     64  TRUE
9  009 Asian  25   Male 00:30:00 106     70 FALSE
10 010 White  27   Male 00:30:00 121     68 FALSE
.. ...   ... ...    ...      ... ...    ...   ...

有时,无论你如何努力,问题真的无法用较小的数据块再现,而且合成数据也不会发生(尽管展示你是如何生成没有再现问题的合成数据集是有用的,因为它排除了一些假设)。

可能需要将数据发布到web某处并提供URL。如果数据不能向公众公开,但可以共享,那么您可以通过电子邮件将其发送给感兴趣的各方(尽管这将减少需要处理的人数)。我实际上还没有看到这样做,因为无法发布数据的人对以任何形式发布数据都很敏感,但在某些情况下,如果数据在某种程度上被充分匿名/加扰/轻微损坏,人们仍然可以发布数据。

如果你不能做到这两个,那么你可能需要聘请一位顾问来解决你的问题。。。

编辑:匿名/加扰的两个有用SO问题:

如何从私有数据创建示例数据集(用无信息的占位符替换变量名称和级别)?给定一组从连续单变量分布中抽取的随机数,找到分布