# [R] Advice on parsing formulae

Claus Dethlefsen dethlef at math.aau.dk
Mon Dec 13 17:10:29 CET 2004

```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)
example:

> model.matrix(Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3)))
(Intercept)     z     u B2 poly(v, 3)1 poly(v, 3)2 poly(v, 3)3  x:A1
x:A2
1            1 -1.55 -1.03  0       0.160      -0.350      -0.281  0.66
0.00
2            1 -1.08  0.55  0      -0.164      -0.211       0.340  0.91
0.00
3            1  0.29 -0.26  0      -0.236      -0.073       0.311 -1.93
0.00
4            1 -1.11  0.96  0       0.222      -0.285      -0.385 -0.23
0.00
5            1  0.43 -0.76  1      -0.434       0.515      -0.532  0.22
0.00

I would like the vector

c(0,1,0,2,3,3,3,4,4)

pointing to the tvar-grouped terms.

Thus what I would like, looks a bit like the 'assign' attribute of the
model.matrix() output. I have not figured out a way of doing this in a nice
way and would like some help, please.

I hope somebody can help me (or point the manual-pages I should read),

Best,

Claus Dethlefsen
----------------------------------------------------------------------------
---
Assistant Professor, Claus Dethlefsen, Ph.D.
mailto:dethlef at math.auc.dk, http://www.math.auc.dk/~dethlef
Dpt. of Mathematical Sciences, Aalborg University
Fr. Bajers Vej 7G, 9220 Aalborg East
Denmark

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.

```