0

I have two fit objects reg and reg1.

I want to run summary() on each without having to re specify whole thing, just the arguments. What is an easy way to do this in general in R? I tried something like this:

lapply(c(reg, reg1), function(x) summary.default(x))

but note that

> class(reg1)
[1] "glm" "lm" 
> class(reg)
[1] "lm"

regardless of the general solution asked for, is it the different classes that are messing with this particular case? is it the format of the first lapply arg.?

Edit: Figured it out

lapply(list(reg, reg1), summary)

but why does it need to be list()? I see that list(reg) calls lm. why is c(reg, reg1) not an appropriate way to pass arguments?

Thanks

Forevertrip
  • 33
  • 1
  • 1
  • 4
  • `lapply(list(reg,reg1), summary)` should work, giving you a list of length 2. What's the problem that you are having? – RLave Mar 14 '19 at 10:28
  • 1
    To make it more clear, what is messing with this particular case is the use of `c()`, use `list(reg, reg1)`, like @RLave said in his comment. – Rui Barradas Mar 14 '19 at 10:30
  • thanks for that. But why is c() not appropriate? – Forevertrip Mar 14 '19 at 10:47
  • @FacundoFerreira - well by definition lapply only works on lists – MatthewR Mar 14 '19 at 11:02
  • @MatthewR That is not true. For example, `lapply(1:2, function(x) x + 1)` is perfectly valid code, despite the fact that `1:2` is not a list. `lapply` starts with an `l` because it *returns* a list. – IceCreamToucan Mar 14 '19 at 11:24
  • 3
    @FacundoFerreira `glm` and `lm` objects are lists internally. `c` combines these two lists (each containing multiple elements, see `str(reg1)`) into one list, which is obviously not what you want. You want to *put* them into a list and that's why you need `list`. – Roland Mar 14 '19 at 11:28

0 Answers0