为什么这个dplyr dput不工作? [英] Why does this dplyr dput not work?

查看:90
本文介绍了为什么这个dplyr dput不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下管道操作顺序获得的数据框:

  library(dplyr)
data_agg = data%>%
group_by(年,月)%>%
总汇(monthly_users = sum(Users))

head(data_agg)如下所示:

 年月月_us 
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() code>获取:

 结构(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 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(grouping_df,
tbl_df ,tbl,data.frame),vars = list(Year),drop = TRUE)

但是,当我从 dput()运行上面的输出时,我收到以下错误:

 结构错误(list(Year = c(2013​​,2013​​,2013​​,2013​​,2013​​,
/ pre>

对象'年'未找到



为什么会这样?

解决方案

解决方法是更改​​:

 ,vars = list(Year),drop = TRUE 

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

这允许您使用 dput 的结果来重新创建原始输出。比较以下内容。

  mtcars2<  -  mtcars%>%group_by(cyl,gear,carb)%>%summary (mmpg = mean(mpg))
dput(mtcars2)
结构(list(cyl = c(4,4,4,4,6,6,6,8,8,8, 8),齿轮= 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(grouping_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),齿轮= 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(grouping_df,
tbl_df,tbl,data.frame vars = list(quote(cyl),quote(gear)),drop = TRUE)






另一个选项是删除 vars = list(Year),部分 dput 并在您读取数据后再使用重组

  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,齿轮,
carb,mmpg),row.names = c(NA,-12L),class = c(grouping_df,
tbl_df,tbl 。框架))

ungroupedmtcars< - 重组(ungroupedmtcars,list(quote(cyl),quote(gear)))
/ pre>

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?

解决方案

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)))

这篇关于为什么这个dplyr dput不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆