I programmatically evaluated several models, whose names are in a vector models
. How can I then use the function mtable
with them, calling them programmatically?
Here is an example :
library(memisc)
a <- rnorm(100,0,1)
b <- rnorm(100,0,1)
c <- rnorm(100,0,1)
d <- rnorm(100,0,1)
mod1 <- lm(a ~ b)
mod2 <- lm(c ~ d)
models <- c("mod1", "mod2")
mtable(mget(models,envir=globalenv()))
I then get an error: "no method available for 'getSummary' for an object of class 'list'"
.
What can I do? I tried call
and do.call
but without success.
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.
The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.
To extract (also known as indexing or subscripting) one or more values (more generally known as elements) from a vector we use the square bracket [ ] notation.
Almost all data in R is stored in a vector, or even a vector of vectors. A list is a recursive vector: a vector that can contain another vector or list in each of its elements. Lists are one of the most flexible data structures in R.
Without mget()
:
do.call(mtable, lapply(models, as.symbol))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With