When you are working with language elements some of the rules change. You apparently want a character column or two to label your results but what you have constructed in that first "mof" object is a character vector with just the names of the models, not the models themselves.
> str(mof)
'data.frame': 3 obs. of 1 variable:
$ V1: chr "m01" "m02" "m03"
To retrieve the models from the workspace using character vectors you need to use get
. They would then be available for further processing with the functions formula
and as.character
. You then will need to "go back" to character mode at the end, since objects of class-formula are not valid dataframe components. All in one nested call this would be:
> forms.mat <- sapply( lapply( lapply(mof$V1, get) , formula), as.character)
> forms.mat
[,1] [,2] [,3]
[1,] "~" "~" "~"
[2,] "g" "g" "g"
[3,] "ab + I(r^2) + cos(pi * h)" "I(ab^3) + r + cos(pi * h) + sin(pi * h)" "ab + r"
You could rearrange (to get the tilde back in between the LHS and the RHS expressions and paste together (with collapse="") with:
> apply(forms.mat[ c(2,1,3),], 2, paste, collapse="")
[1] "g~ab + I(r^2) + cos(pi * h)" "g~I(ab^3) + r + cos(pi * h) + sin(pi * h)"
[3] "g~ab + r"
You might have simplified this a bit by just working with a list:
> mof2 <- list(m01,m02,m03) # Skipping the search of the workspace and reconstitution
> lapply(mof2, formula)
[[1]]
g ~ ab + I(r^2) + cos(pi * h)
<environment: 0x2ef997c60>
[[2]]
g ~ I(ab^3) + r + cos(pi * h) + sin(pi * h)
<environment: 0x19fef52d0>
[[3]]
g ~ ab + r
<environment: 0x19fef68d8>
> sapply( lapply(mof2, formula), as.character )
[,1] [,2] [,3]
[1,] "~" "~" "~"
[2,] "g" "g" "g"
[3,] "ab + I(r^2) + cos(pi * h)" "I(ab^3) + r + cos(pi * h) + sin(pi * h)" "ab + r"