我有一个使用以下管道操作序列获得的数据框:
library(dplyr)
data_agg = data %>%
group_by(Year,Month) %>%
summarise( monthly_users = sum(Users))
head(data_agg)
看起来像这样:
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
我现在dput()
,获得:
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)
但是,当我从dput()
运行上述输出时,我收到以下错误:
Error in structure(list(Year = c("2013", "2013", "2013", "2013", "2013", :
找不到对象'年'
为什么会这样?
答案 0 :(得分:5)
解决方法是更改:
, vars = list(Year), drop = TRUE
到
, vars = list(quote(Year)), drop = TRUE
这允许您使用dput
的结果重新创建原始输出。比较以下内容。
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)
另一种选择是删除&#34; vars = list(Year),
&#34;在您重新阅读数据后,dput
的一部分并使用regroup
。
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)))