Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

r - Why does this dplyr dput not work?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...