【问题标题】:Getting a command to re-create an object that is simpler than with dput()获取一个命令来重新创建一个比使用 dput() 更简单的对象
【发布时间】:2014-07-28 07:41:13
【问题描述】:

dput 的输出通常比用户为创建相同对象而键入的内容要复杂得多。我了解这可能是保证 100% 可重复性所必需的(例如,当不同的用户使用不同的默认设置时)。但是,它并没有使示例尽可能具有可读性,而且我经常花一些时间来简化输出。

例如,考虑:

dput(data.frame(a=1:10))
> structure(list(a = 1:10), .Names = "a", row.names = c(NA, -10L), class = "data.frame")

难道没有dput 的替代方案可以简单地返回data.frame(a=1:10) 吗?

【问题讨论】:

  • 我不明白你为什么担心dput 输出的复杂性,因为它不是用来读取的,而是用来复制和粘贴的——在这种情况下,复杂性对我来说并不重要意见。
  • 要获得更易读的表示,请使用str(data.frame(a = 1:10))
  • @beginneR 如果它打算嵌入到 stackoverflow 问题中,那么可重现和可读比仅仅可重现更好。
  • @tonytonov 我应该更新我的问题标题...我不想要任何表示,而是要重新创建对象的命令。对不起,我忘了提。
  • 你学习过How to make a great R reproducible example吗?在我看来,这可能是重复的。

标签: r reproducible-research


【解决方案1】:

这是 data.frames 的部分实现(没有 row.names 或其他属性):

dput2 <- function(x, ...) UseMethod("dput2")
dput2.data.frame <- function(x, ...) {
    fun <- function(nm) paste(nm, "=", 
            paste(capture.output(dput(x[[nm]], file = stdout())), collapse = ""))
    L <- lapply(names(x), fun)
    cat(paste("data.frame(", paste(unlist(L), collapse = ",\n"), ")"), "\n")
}

例如,

> dput2(BOD)
data.frame( Time = c(1, 2, 3, 4, 5, 7),
demand = c(8.3, 10.3, 19, 16, 15.6, 19.8) ) 

> dput2(anscombe)
data.frame( x1 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
x2 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
x3 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
x4 = c(8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8),
y1 = c(8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68),
y2 = c(9.14, 8.14, 8.74, 8.77, 9.26, 8.1, 6.13, 3.1, 9.13, 7.26, 4.74),
y3 = c(7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73),
y4 = c(6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.5, 5.56, 7.91, 6.89) ) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多