Berend et. al:

1, Except you did not use "my solution." It is not what you have but
instead:

f4 <- function(df)na.omit.df

wrapping the return in data.frame() is both unnecessary and inefficient.

2. But my point is not to be the speediest nor even to show how clever I am
-- using a built-in function is hardly being clever. Rather to emphasize
that the OP requested a standard task for which R ALREADY HAS BUILT IN
FUNCTIONALITY.  Reading ?na.omit shows that the returned object contains
information (about the omitted rows) that other R functions (e.g.
predict()  ) make use of.  That is the point. Rather than "roll your own,"
one should use that functionality. It is generally there for a good reason,
as in this case.

Cheers,
Bert


On Thu, Nov 22, 2012 at 8:03 AM, Berend Hasselman <bhh@xs4all.nl> wrote:

>
> On 22-11-2012, at 16:50, Muhuri, Pradip (SAMHSA/CBHSQ) wrote:
>
> > Hi Berend,
> >
> > You have compared all 3 ways.  ... very nicely evaluated.
> >
>
> Bert's solution is indeed nice and simple. But Petr's solution is still
> the quickest:
>
> >N <- 100000
> > set.seed(13)
> > df <- data.frame(matrix(sample(c(1:10,NA),N,replace=TRUE),ncol=50))
> > library(rbenchmark)
> >
> > f1 <- function(df) {df[apply(df, 1, function(x)all(!is.na(x))),]}
> > f2 <- function(df) {df[!is.na(rowSums(df)),]}
> > f3 <- function(df) {df[complete.cases(df),]}
> > f4 <- function(df) {data.frame(na.omit(df))}
> > benchmark(d1 <- f1(df), d2 <- f2(df), d3 <- f3(df), d4 <- f4(df),
> columns=c("test","elapsed", "relative", "replications"))
>           test elapsed relative replications
> 1 d1 <- f1(df)   3.588   14.888          100
> 2 d2 <- f2(df)   0.403    1.672          100
> 3 d3 <- f3(df)   0.241    1.000          100
> 4 d4 <- f4(df)   0.557    2.311          100
> >
> > identical(d1,d2)
> [1] TRUE
> > identical(d1,d3)
> [1] TRUE
> > identical(d1,d4)
> [1] TRUE
>
> Berend
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

	[[alternative HTML version deleted]]


