可重现的示例和输入错误

时间:2015-03-13 18:35:34

标签: r dplyr

我正在尝试重现数据框,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并且所有工作都可以。

1 个答案:

答案 0 :(得分:4)

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

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

新Data.frame:

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))