Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

r - Reproducible example and dput error

I'm trying to reproduce a data frame and dput is not cooperating.

dput command :

dput(head(data, 10))

dput output :

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 :

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

Why is this happening right from a dput command?

Edit :

Relevant posts, but suggestions did not work.

Why does this dplyr dput not work?

Edit 2 :

It appears because one of my variables is a group object, dput cannot reproduce this. The solution is to use ungroup(data) then rerun dput and all works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue was one of the variable objects was a group and therefore, dput() couldn't recognize this. The solution was to ungroup() the data.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...