[R] expanding factor with NA
Thomas W Blackwell
tblackw at umich.edu
Mon Oct 27 20:59:59 CET 2003
I would re-expand the model matrix by indexing its (nobs) rows
with a longer vector (of length n) containing the correspondence.
If there is only one term (say "Z") in the formula which contains
the problematic NAs, I would do (roughly)
ff <- Y ~ Z # following the example in ?model.matrix
mat <- model.matrix(ff, model.frame(ff, data))[cumsum(!is.na(Z)), ]
mat[is.na(Z), ] <- NA
The second line above creates an n x k matrix in which each row
where Z has NA simply duplicates the last preceding non-NA row.
The third line above blanks out those duplicate rows by filling
them with NAs instead. This simple strategy fails if Z[1] is NA.
I haven't time to think up a solution for that case, other than
permuting rows in the entire data set so that it doesn't happen.
HTH - tom blackwell - u michigan medical school - ann arbor -
On Mon, 27 Oct 2003, J.R. Lockwood wrote:
> I have a factor (with "n" observations and "k" levels), with only
> "nobs" < n of the observations not missing. I would like to produce a
> (n x k) model matrix with treatment contrasts for this factor, with
> rows of NAs placeholding the missing observations. If I use
> model.matrix() I get back a (nobs x k) matrix. Is there an easy way
> to get the (n x k) without carrying along a row ID and merging?
> Thanks.
>
> J.R. Lockwood
> 412-683-2300 x4941
> lockwood at rand.org
> http://www.rand.org/methodology/stat/members/lockwood/
More information about the R-help
mailing list