我有一个使用以下管道操作序列获得的数据框:
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", :
未找到对象“年份”
为什么会这样?
最佳答案
解决方法是更改:
, 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)
另一种选择是删除
vars = list(Year),
的“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)))
关于r - 为什么这个 dplyr dput 不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25151796/