【发布时间】:2015-04-27 00:26:19
【问题描述】:
我正在使用do() 将模型拟合到分组数据,然后我想为每个组绘制拟合图。在plyr 中,我想我会使用d_ply()。在dplyr 中,我正在尝试do() 或summarise() 使用使情节成为副作用的函数。
根据我是使用do() 还是summarise(),我得到不同的结果,我不知道为什么。具体来说,summarise() 似乎没有在每一行上正确运行。
这是我的例子:
require(nycflights13)
require(mgcv)
# fit a gam to the flights grouped by dest (from ?do)
by_dest <- flights %>% group_by(dest) %>% filter(n() > 100)
models = by_dest %>% do(smooth = gam(arr_delay ~ s(dep_time) + month, data = .))
# print the first 4 rows, the dest is ABQ, ACK, ALB, ATL
models %>% slice(1:4)
# make a function to plot the models, titled by dest
plot.w.title = function(title, gam.model){
plot.gam(gam.model, main=title)
return(1)
}
# This code makes plots with the wrong titles, for example ATL is listed twice:
models %>%
slice(1:4) %>%
rowwise %>%
summarise(useless.column = plot.w.title(dest, smooth)) # for plot side effect
# this code gives me the correct titles...why the difference?
models %>%
slice(1:4) %>%
rowwise %>%
do(useless.column = plot.w.title(.$dest, .$smooth))
【问题讨论】:
-
当我运行 summarise() 代码时,我没有看到 ATL 两次。我看到 ABQ、ACK、ALB 和 ATL。您如何看待这些情节?
-
这可能是
RStudio问题。当我使用x11或图形设备时,一切正常,但在RStudio图形设备中我遇到了同样的问题。 -
它在 Rstudio 图形设备上……我什至没有想到。谢谢!