在与同事讨论绩效、教学、发送错误报告或搜索邮件列表和Stack Overflow上的指导时,经常会询问一个可重复的示例,并且总是很有用。
你有什么建议来创建一个优秀的例子?如何以文本格式粘贴r中的数据结构?您还应包括哪些其他信息?
除了使用dput()、dump()或structure()之外,还有其他技巧吗?什么时候应该包含library()或require()语句?除了c、df、data等之外,应该避免哪些保留字。?
如何做出一个伟大的、可重复的例子?
在与同事讨论绩效、教学、发送错误报告或搜索邮件列表和Stack Overflow上的指导时,经常会询问一个可重复的示例,并且总是很有用。
你有什么建议来创建一个优秀的例子?如何以文本格式粘贴r中的数据结构?您还应包括哪些其他信息?
除了使用dput()、dump()或structure()之外,还有其他技巧吗?什么时候应该包含library()或require()语句?除了c、df、data等之外,应该避免哪些保留字。?
如何做出一个伟大的、可重复的例子?
当前回答
有时,无论你如何努力,问题真的无法用较小的数据块再现,而且合成数据也不会发生(尽管展示你是如何生成没有再现问题的合成数据集是有用的,因为它排除了一些假设)。
可能需要将数据发布到web某处并提供URL。如果数据不能向公众公开,但可以共享,那么您可以通过电子邮件将其发送给感兴趣的各方(尽管这将减少需要处理的人数)。我实际上还没有看到这样做,因为无法发布数据的人对以任何形式发布数据都很敏感,但在某些情况下,如果数据在某种程度上被充分匿名/加扰/轻微损坏,人们仍然可以发布数据。
如果你不能做到这两个,那么你可能需要聘请一位顾问来解决你的问题。。。
编辑:匿名/加扰的两个有用SO问题:
如何从私有数据创建示例数据集(用无信息的占位符替换变量名称和级别)?给定一组从连续单变量分布中抽取的随机数,找到分布
其他回答
就我个人而言,我更喜欢“一”行。大致如下:
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")
如果您的数据中有一个或多个因子变量,您希望使用dput(head(mydata))进行复制,请考虑向其添加droplevel,以便最小化数据集中不存在的因子级别不包含在dput输出中,以使示例最小化:
dput(droplevels(head(mydata)))
请不要像这样粘贴控制台输出:
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")
还有一件事,如果您使用了某个包中的任何函数,请提及该库。
受到这篇文章的启发,我现在使用了一个方便的功能,当我需要发布到堆栈溢出时,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==
以下是我的一些建议:
尝试使用默认的R数据集如果您有自己的数据集,请将其包含在dput中,这样其他人可以更轻松地帮助您除非确有必要,否则不要使用install.package(),人们会理解您是否只使用require或library尽量简明扼要,有一些数据集尽量简单地描述您需要的输出问问题之前自己做上传图片很容易,所以如果你有还包括您可能遇到的任何错误
所有这些都是可复制示例的一部分。