作为一名新用户,因为无法重现问题,我的问题没有得到完全回答。我读了与生成可重现代码相关的线程,但并没有成功。特别是对如何使用 dput() 函数感到困惑。能否有人提供一个使用 dput() 函数的逐步说明,例如使用 iris 数据框,这将非常有帮助。
使用方便地包含在R
中的iris
数据集,我们可以看到dput()
如何工作:
data(iris)
head(iris)
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(iris)
来获取整个数据集。在大多数情况下,提供整个数据集对于Stackoverflow问题是不必要的,因为几行相关变量就足以作为工作数据示例。
有两件事情非常方便: head()
函数仅输出数据帧/矩阵的前六行。此外,在R
中使用索引(通过括号)可以选择特定列。
因此,我们可以使用这两种方法的组合来限制dput()
的输出:
dput(head(iris[, c(1, 3)]))
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7)), .Names = c("Sepal.Length",
"Petal.Length"), row.names = c(NA, 6L), class = "data.frame")
将会给我们提供复制前6行第1列和第3列的代码,用于iris
数据集。
df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7)), .Names = c("Sepal.Length",
"Petal.Length"), row.names = c(NA, 6L), class = "data.frame")
> df
Sepal.Length Petal.Length
1 5.1 1.4
2 4.9 1.4
3 4.7 1.3
4 4.6 1.5
5 5.0 1.4
6 5.4 1.7
如果第一行不足够,我们可以跳过使用head()
,仅依赖索引:
dput(iris[1:20, c(1, 3)])
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6,
5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1
), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4,
1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5)), .Names = c("Sepal.Length",
"Petal.Length"), row.names = c(NA, 20L), class = "data.frame")
将为我们提供前二十行:
df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6,
5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1
), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4,
1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5)), .Names = c("Sepal.Length",
"Petal.Length"), row.names = c(NA, 20L), class = "data.frame")
> df
Sepal.Length Petal.Length
1 5.1 1.4
2 4.9 1.4
3 4.7 1.3
4 4.6 1.5
5 5.0 1.4
6 5.4 1.7
7 4.6 1.4
8 5.0 1.5
9 4.4 1.4
10 4.9 1.5
11 5.4 1.5
12 4.8 1.6
13 4.8 1.4
14 4.3 1.1
15 5.8 1.2
16 5.7 1.5
17 5.4 1.3
18 5.1 1.4
19 5.7 1.7
20 5.1 1.5
structure()
。 - LAP
df
,您只需在R中输入dput(df)
或dput(iris)
,然后复制输出并粘贴到此处。我相信您指的是这个链接。 - Ronak Shahdput
的示例。 - mt1022dput(head(iris, 10))
(输出前10行)来展示问题。 - neilfwsdput
函数,即dput(df[1:10, c(86, 88, 99)])
。 - Ronak Shah