2015-03-13 47 views
2

我試圖重現數據幀和dput不合作。可重複的示例和輸入錯誤

dput命令:

dput(head(data, 10)) 

dput輸出:

structure(list(lexptot = c(8.28377505197124, 9.1595, 
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.1595, : 
    object 'nh' not found 

這是爲什麼從dput命令發生吧?

編輯:

相關帖子,但建議沒有奏效。

Why does this dplyr dput not work?

編輯2:

看來,因爲我的變量之一是group對象,dput無法重現此。解決方法是使用ungroup(data)然後重新運行dput和所有的作品。

+0

我已經添加了dplyr標籤,因爲這可能是你的問題的根源。由於我不使用dplyr,因此無法進一步幫助您。 – Roland 2015-03-13 18:40:16

+0

@Roland爲什麼這是一個dplyr問題? – Vedda 2015-03-13 18:41:50

+1

如果您擁有「正常」數據框,通常情況下不會出現此問題。但你有一個'grouped_df',這很可能是相關的。我在那裏看到'vars = list(nh)',這是對另一個通常不是'dput'輸出的一部分的對象的引用。 – Roland 2015-03-13 18:44:32

回答

4

該問題是其中一個變量對象是group,因此dput()無法識別此問題。解決方案是給ungroup()的數據。

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

新Data.frame:

structure(list(lexptot = c(8.28377505197124, 9.1595, 
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)) 
+0

我最近指出了這個答案,但是我必須做'dput(ungroup(data))''。這應該編輯? – raphael 2015-05-05 05:12:32

+0

@raphael我是另一種選擇。問題是數據框中的「組」變量。無論哪種方式應該工作。 – Vedda 2015-05-05 19:03:28