【问题标题】:Reproducible example and dput error可重现的示例和输入错误
【发布时间】:2015-03-16 04:15:08
【问题描述】:

我正在尝试重现数据框,但 dput 不合作。

dput 命令:

dput(head(data, 10))

dput 输出:

structure(list(lexptot = c(8.28377505197124, 9.1595012302023, 
8.14707583238833, 9.86330744180814, 8.21391453619232, 8.92372556833205, 
7.77219149815994, 8.58202430280175, 8.34096828565733, 10.1133857229336
), year = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), dfmfdyr = c(0, 
1, 0, 1, 0, 1, 0, 1, 0, 1), dfmfd98 = c(1, 1, 1, 1, 1, 1, 1, 
1, 1, 1), nh = c(11054L, 11054L, 11061L, 11061L, 11081L, 11081L, 
11101L, 11101L, 12021L, 12021L)), .Names = c("lexptot", "year", 
"dfmfdyr", "dfmfd98", "nh"), vars = list(nh), drop = TRUE, indices = list(
0:1, 2:3, 4:5, 6:7, 8:9), group_sizes = c(2L, 2L, 2L, 2L, 
2L), biggest_group_size = 2L, labels = structure(list(nh = c(11054L, 
11061L, 11081L, 11101L, 12021L)), class = "data.frame", row.names = c(NA, 
-5L), .Names = "nh", vars = list(nh)), row.names = c(NA, 10L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

错误:

Error in structure(list(lexptot = c(8.28377505197124, 9.1595012302023,  : 
  object 'nh' not found

为什么在 dput 命令中会发生这种情况?

编辑:

相关帖子,但建议无效。

Why does this dplyr dput not work?

编辑 2:

似乎是因为我的变量之一是group 对象,dput 无法重现这一点。解决方案是使用 ungroup(data) 然后重新运行 dput 并且一切正常。

【问题讨论】:

  • 我添加了 dplyr 标签,因为这可能是您问题的根源。由于我不使用 dplyr,因此无法进一步帮助您。
  • @Roland 为什么这是一个 dplyr 问题?
  • 如果你有一个“正常”的data.frame,你通常不会有这个问题。但是您拥有grouped_df,这很可能是相关的。我在那里看到vars = list(nh),这是对另一个对象的引用,该对象通常不属于dput 输出。
  • 空格是由于 RStudio 的自动缩进加上不幸的换行符放置(大量中间向量中断,很少向量间中断)。如果您愿意,可以在菜单工具/全局选项/代码编辑中关闭“粘贴后自动缩进”。
  • 您不能dput 对象是/包含对其他对象的引用。这是您为性能付出的代价。

标签: r dplyr


【解决方案1】:

问题是变量对象之一是group,因此dput() 无法识别这一点。解决方案是ungroup() 数据。

ungroup(data)
dput(head(data, 10))

新数据框架:

structure(list(lexptot = c(8.28377505197124, 9.1595012302023, 
8.14707583238833, 9.86330744180814, 8.21391453619232, 8.92372556833205, 
7.77219149815994, 8.58202430280175, 8.34096828565733, 10.1133857229336
), year = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), dfmfd98 = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1), dfmfd = c(0L, 1L, 0L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L)), .Names = c("lexptot", "year", "dfmfd98", "dfmfd"
), class = c("tbl_df", "data.frame"), row.names = c(NA, -10L))

【讨论】:

  • 我最近被指出了这个答案,但我必须这样做dput(ungroup(data))。应该编辑吗?
  • @raphael 我想换个选择。问题是数据框中的“组”变量。无论哪种方式都应该有效。
猜你喜欢
  • 2020-03-12
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多