过滤在dplyr中创建的模型表时出错

时间:2021-08-13 22:25:53

I'm working with lists of models generated in dplyr

我正在使用dplyr中生成的模型列表

library(dplyr)
library(magrittr)

mod.list <- iris %>%
  group_by(Species) %>%
  do(mod = lm(Petal.Length ~ Petal.Width, data = .))

If I plot every model, all works as expected

如果我绘制每个模型,所有都按预期工作

par(mfrow = c(2,2))

mod.list %>%
  do(.$mod %>% plot)

However if I introduce a filter, I get an error

但是,如果我引入过滤器,我会收到错误

mod.list %>%
  filter(row_number() <= 1) %>%
  do(.$mod %>% plot)

Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'

I've also tried another type of filter, but the error is the same

我也尝试过其他类型的过滤器,但错误是一样的

mod.list %>%
  filter(Species == "setosa") %>%
  do(.$mod %>% plot)

Does anyone know why this is happening?

有谁知道为什么会这样?

1 个解决方案

#1


1  

To debug, you can use

要进行调试,您可以使用

mod.list %>%
  filter(row_number() <= 1) %>%
  do(.$mod  %>% (function(x) browser()))

Then you see that class(x) is a list. You want the first (only) element, so

然后你看到class(x)是一个列表。你想要第一个(唯一的)元素,所以

mod.list %>%
  filter(row_number() <= 1) %>%
  do(.$mod  %>% `[[`(i=1) %>% plot)

#1


1  

To debug, you can use

要进行调试,您可以使用

mod.list %>%
  filter(row_number() <= 1) %>%
  do(.$mod  %>% (function(x) browser()))

Then you see that class(x) is a list. You want the first (only) element, so

然后你看到class(x)是一个列表。你想要第一个(唯一的)元素,所以

mod.list %>%
  filter(row_number() <= 1) %>%
  do(.$mod  %>% `[[`(i=1) %>% plot)