【问题标题】:Example of using dput()使用 dput() 的示例
【发布时间】:2023-04-01 14:39:02
【问题描述】:

作为这里的新用户,由于不可重现,我的问题没有得到完全回答。我阅读了与生成可重现代码有关的线程,但没有用。特别迷失了如何使用 dput() 函数。

有人可以提供有关如何使用 iris df 使用 dput() 的分步说明,例如这将非常有帮助。

【问题讨论】:

  • 如果您的数据框是df,您只需要在您的情况下执行dput(df)dput(iris) 并复制输出并将其粘贴到此处,我相信您的意思是this link
  • 我建议你read it again。它确实有一些dput 的例子。
  • 如果数据框很大,但前几行足以说明您的问题,您可以这样做 eg dput(head(iris, 10))(前 10 行)。
  • 是的,我的 df 是 masssize,有 100 多列。我如何也选择特定的列.. 例如第 86 列、第 88 列和第 99 列?谢谢!
  • 子集数据帧,然后使用dput,即dput(df[1:10, c(86, 88, 99)])

标签: r reproducible-research


【解决方案1】:

使用iris 数据集,它很容易包含在R 中,我们可以看到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")

将为我们提供重现 iris 数据集第 1 列和第 3 列的前(最多)六行的代码。

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

【讨论】:

  • 我仍然不确定,如果我想在 stackoverflow 中发布我的 MWE,下一步该怎么做。应该只是复制并粘贴“结构()”中的内容
  • @msh855 是,包括structure()
猜你喜欢
  • 2018-05-07
  • 2022-08-18
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多