[R] Advice on parsing formulae

Chuck Berry cberry at tajo.ucsd.edu
Tue Dec 14 20:04:15 CET 2004


Claus Dethlefsen <dethlef <at> math.aau.dk> writes: 
 
>  
> Dear list 
>  
> I would like to be able to group terms in a formula using a function that I 
> will call tvar(), eg. the formula 
>  
> Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3)) 
>  
> where x,u and v are numeric and A and B are factors - binary, say. 
>  
> As output, I want the model.matrix as if tvar had not been there at all. In 
> addition, I would like to have information on the grouping, as a vector as 
> long as ncol( model.matrix ) with zeros corresponding to terms outside tvar 
> and with an index grouping the terms inside each tvar(). In the (sick) 
 
 
Since you want to single out terms with 'tvar' in them, something like 
 
tvar.terms <- terms( your.formula.above, specials = "tvar" ) 
 
will likely be what you want. You'll want to study 
 
    ?terms 
    ?terms.object 
 
and then 
 
    print( tvar.terms ) 
 
And likely 'deparse' will help, too. 
 
> example: 
[rest deleted]




More information about the R-help mailing list