Like, Was: Re: [R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
Don MacQueen
macq at llnl.gov
Tue May 7 01:55:10 CEST 2002
More than once, including in a project I'm currently working on, I
could really use such a function. If you don't mind I'll make a local
copy and use it until it hopefully gets into R. Want me to start a
help page?
Good that it preserves mode and data.class, including "AsIs".
-Don
At 11:19 PM +0200 5/6/02, Peter Dalgaard BSA wrote:
>"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
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
--------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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