2

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.

Community
  • 1
  • 1
Vedda
  • 7,066
  • 6
  • 42
  • 77
  • I've added the dplyr tag, since that is probably the root of your problem. Can't help you further since I don't use dplyr. – Roland Mar 13 '15 at 18:40
  • @Roland why would this be a dplyr problem? – Vedda Mar 13 '15 at 18:41
  • 1
    You normally wouldn't have this problem if you had a "normal" data.frame. But you habe a `grouped_df` and that's very likely related. I see `vars = list(nh)` in there and that's a reference to another object which is normally not part of `dput` output. – Roland Mar 13 '15 at 18:44
  • 1
    The spaces are due to RStudio's auto-indentation coupled with unfortunate placements of line breaks (lots of mid-vector breaks, very few between-vector breaks). In the menu Tools/Global Options/Code Editing you can turn off "auto-indent after paste" if you'd like. – Gregor Thomas Mar 13 '15 at 18:45
  • I'm removing the RStudio part of the question (and tag) because it's completely independent of the error. If you want more info on that, ask a separate question. – Gregor Thomas Mar 13 '15 at 18:47
  • For a reproducible example, `dput(group_by(mtcars, cyl))` has the same issue. Might need to be reported as a `dplyr` issue. – Gregor Thomas Mar 13 '15 at 18:52
  • 1
    You can't `dput` objects that are/contain references to other objects. That's a price you pay for performance. – Roland Mar 13 '15 at 18:57
  • Or use `dump(list = "variable_name", file = stdout())` – bergant Mar 14 '15 at 00:22

1 Answers1

4

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))
Vedda
  • 7,066
  • 6
  • 42
  • 77
  • I was recently pointed to this answer, but I had to do `dput(ungroup(data))`. Should this be edited? – raphael May 05 '15 at 05:12
  • @raphael that I'd another alternative. The problem is the 'group' variables in the data frame. Either way should work. – Vedda May 05 '15 at 19:03