Like, Was: Re: [R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon May 6 23:19:48 CEST 2002


"David Kane  <David Kane" <a296180 at mica.fmr.com> writes:

> > x <- data.frame(a = letters[1:3], b = 1:3)
> > x
>   a b
> 1 a 1
> 2 b 2
> 3 c 3
> > x[4,]
>     a  b
> NA NA NA
> > rbind(x, x[4,])
>     a  b
> 1   a  1
> 2   b  2
> 3   c  3
> NA NA NA
....
> This does not work in 1.5.0
....

Brian fixed this for 1.5.1. However, the whole idea helped me with
something that has been bugging me for years: 

When you want to predict on new data

foo <- lm(formula, data=foe)
predict(foo, newdata=fie)

you have to be careful that foe and fie  are compatible, all factors
have the same level set, and so forth. This can be quite a pain, so I
wanted a "like" function as in 

fie <- like(foe, sex="F", age=20:25, smoke="1-7")

but only after seeing the above way of making "blank" rows, it dawned
upon me that such rows have exactly the properties I wanted.
Elaborating a little, I got 

like <- function(X,...){ 
    l <- list(...)
    n <- max(sapply(l, length))
    X <- X[rep(as.numeric(NA),n),]
    row.names(X) <- seq(length=n)
    for (nm in names(l)) X[[nm]][] <- l[[nm]]
    X
}

A six-liner! Neat, eh? Probably has a good chance of making it into
1.6 once I get around to writing a help page.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list