3

I have a data frame obtained using the following sequence of pipe operations:

library(dplyr)
data_agg = data %>%
    group_by(Year,Month) %>%
    summarise( monthly_users = sum(Users))

head(data_agg) looks like this:

  Year Month monthly_users
1 2013    07            22
2 2013    08           221
3 2013    09           252
4 2013    10           313
5 2013    11           322
6 2013    12           339

I now dput() it, obtaining:

structure(list(Year = c("2013", "2013", "2013", "2013", "2013", 
"2013", "2014", "2014", "2014", "2014", "2014", "2014", "2014"
), Month = c("07", "08", "09", "10", "11", "12", "01", "02", 
"03", "04", "05", "06", "07"), monthly_users = c(22L, 221L, 252L, 
313L, 322L, 339L, 344L, 338L, 301L, 307L, 401L, 383L, 318L)), .Names = c("Year", 
"Month", "monthly_users"), row.names = c(NA, -13L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), vars = list(Year), drop = TRUE)

However, when I run the above output from dput() I get the following error:

Error in structure(list(Year = c("2013", "2013", "2013", "2013", "2013",  : 

object 'Year' not found

Why is this happening?

Alex
  • 15,186
  • 15
  • 73
  • 127
  • 2
    Delete the `, vars = list(Year), drop = TRUE` part and see if it gives you what you expect. – A5C1D2H2I1M1N2O1R2T1 Aug 06 '14 at 03:36
  • 3
    Looks to be an unevaluated piece of code floating there in the attributes. `attr(data_agg,"vars") <- NULL` will kill it and make it work, though I'm not sure if this has any unintended effects. `str(attr(data_agg,"vars")[[1]])` gives `symbol Year` – thelatemail Aug 06 '14 at 03:39
  • 2
    I suspect this is a similar problem to the `` issue with some `dput`s from "data.table". What is lost is that the `dput` doesn't have the "group" information. To get around this, I used `quote(Year)` and it seems to show that these are now identical. – A5C1D2H2I1M1N2O1R2T1 Aug 06 '14 at 03:56
  • These both seem like valid fixes; please post them as answers so I can vote up. – Alex Aug 06 '14 at 04:11
  • 1
    `dput2` will dput it as an ordinary data frame removing the `vars` part. It is defined here: http://stackoverflow.com/questions/24990572/getting-a-command-to-re-create-an-object-that-is-simpler-than-with-dput/24996330?noredirect=1#comment38894898_24996330 – G. Grothendieck Aug 06 '14 at 11:22

1 Answers1

5

A workaround is to change:

, vars = list(Year), drop = TRUE

to

, vars = list(quote(Year)), drop = TRUE

This allows you to use the result of dput to recreate your original output. Compare the following.

mtcars2 <- mtcars %>% group_by(cyl, gear, carb) %>% summarise(mmpg = mean(mpg))
dput(mtcars2)
structure(list(cyl = c(4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 8, 8), gear = c(3,
4, 4, 5, 3, 4, 5, 3, 3, 3, 5, 5), carb = c(1, 1, 2, 2, 1, 4,
6, 2, 3, 4, 4, 8), mmpg = c(21.5, 29.1, 24.75, 28.2, 19.75, 19.75,
19.7, 17.15, 16.3, 12.62, 15.8, 15)), .Names = c("cyl", "gear",
"carb", "mmpg"), row.names = c(NA, -12L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = list(cyl, gear), drop = TRUE)

newmtcars <- structure(list(cyl = c(4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 8, 8), gear = c(3,
4, 4, 5, 3, 4, 5, 3, 3, 3, 5, 5), carb = c(1, 1, 2, 2, 1, 4,
6, 2, 3, 4, 4, 8), mmpg = c(21.5, 29.1, 24.75, 28.2, 19.75, 19.75,
19.7, 17.15, 16.3, 12.62, 15.8, 15)), .Names = c("cyl", "gear",
"carb", "mmpg"), row.names = c(NA, -12L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = list(quote(cyl), quote(gear)), drop = TRUE)

Another option is to remove the "vars = list(Year)," part of the dput and use regroup after you have read the data back in.

ungroupedmtcars <- structure(list(cyl = c(4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 8, 8), gear = c(3,
4, 4, 5, 3, 4, 5, 3, 3, 3, 5, 5), carb = c(1, 1, 2, 2, 1, 4,
6, 2, 3, 4, 4, 8), mmpg = c(21.5, 29.1, 24.75, 28.2, 19.75, 19.75,
19.7, 17.15, 16.3, 12.62, 15.8, 15)), .Names = c("cyl", "gear",
"carb", "mmpg"), row.names = c(NA, -12L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), drop = TRUE)

ungroupedmtcars <- regroup(ungroupedmtcars, list(quote(cyl), quote(gear)))
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485