I have a formula object and trying to access the formula, I have found the function term:

R) terms(myFormula)
z ~ 0 + I(x/v) + y + I(w/v)
attr(,"variables")
list(z, I(x/v), y, I(w/v))
attr(,"factors")
             I(x/v)      y       I(w/v)
z                 0      0            0
I(x/v)            1      0            0
y                 0      1            0
I(w/v)            0      0            1
attr(,"term.labels")
[1] "I(x/v)"   "y"       "I(w/v)"
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 0
attr(,"response")
[1] 1
attr(,".Environment")
<environment: 0x2f90dee0>

How can I directly access all this data, say the 'factors' or the nicely displayed z ~ 0 + I(x/v) + y + I(w/v) (I mean without using paste all the time).

Additionally what functions should I look to deal with formulas?

有帮助吗?

解决方案

You can get or set attributes with the attr function.

attr(terms(myFormula), "factors")

You can find available methods for formulae using the methods function.

methods(class = "formula")
##  [1] [.formula*             aggregate.formula*     alias.formula*         all.equal.formula     
##  [5] ansari.test.formula*   bartlett.test.formula* boxplot.formula*       cdplot.formula*       
##  [9] cor.test.formula*      deriv.formula          deriv3.formula         fligner.test.formula* 
## [13] formula.formula*       friedman.test.formula* ftable.formula*        getInitial.formula*   
## [17] kruskal.test.formula*  lines.formula*         mood.test.formula*     mosaicplot.formula*   
## [21] pairs.formula*         plot.formula*          points.formula*        ppr.formula*          
## [25] prcomp.formula*        princomp.formula*      print.formula          quade.test.formula*   
## [29] selfStart.formula*     spineplot.formula*     stripchart.formula*    sunflowerplot.formula*
## [33] t.test.formula*        terms.formula          text.formula*          update.formula        
## [37] var.test.formula*      wilcox.test.formula*
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top