Why does this dplyr dput not work?

前端 未结 1 456
忘了有多久
忘了有多久 2020-12-19 18:50

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

library(dplyr)
data_agg = data %>%
    group_by(Year,Month) %>%
    summa         


        
相关标签:
1条回答
  • 2020-12-19 19:22

    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)))
    
    0 讨论(0)
提交回复
热议问题