[R] elegant way of removing NA's and selecting specific values from a data.frame

William Dunlap wdunlap at tibco.com
Wed Jul 6 20:43:59 CEST 2011


The question was whether you could do this
'in a single line' and the word 'elegant' was in the
subject line.  Those two things don't always go together.

You can put semicolons between the statements so they
all can go on one line, but that isn't very elegant.

You could collapse the three assignments into one, as in
  third <- e[,23][!is.na(e[,23])][e[,23][!is.na(e[,23])] < 30]
but that wastes time repeatedly calculating e[,23] and is.na(...)
and is hard to read, both of which count against its
elegance score.

One could use & to go from 3 assignments to 2, as in
  first <- e[,23]
  third <- first[!is.na(first) & first < 30]

You could write a function that returns TRUE where
its logical input vector is TRUE and FALSE where
it is NA or FALSE:
  is.true <- function(x) !is.na(x) & x
and use it as
  first <- e[,23]
  third <- first[is.true(first < 30)]

To my eyes the last is the most elegant way to do things,
as the flow of data is very clear.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter
> Sent: Wednesday, July 06, 2011 8:50 AM
> To: Jim Maas
> Cc: r-help at r-project.org
> Subject: Re: [R] elegant way of removing NA's and selecting 
> specific values from a data.frame
> 
> ?"&"
> 
> This is basic. Please read "An Intro to R" before posting any more
> such questions if you have not already done so.
> 
> -- Bert
> 
> On Wed, Jul 6, 2011 at 8:35 AM, Jim Maas <j.maas at uea.ac.uk> wrote:
> > I have a data.frame "e" and would like to extract the 23rd 
> column, remove
> > any NA's and then remove any values >= 30.  I can do it in 
> steps such as
> > this but have failed to figure out how to do it in a single 
> line .... any
> > suggestions?
> >
> > first <- e[,23]
> > second <- first[!is.na(first)]
> > third <- second[second<=30]
> >
> > thanks a bunch
> >
> > J
> >
> > --
> > Dr. Jim Maas
> > University of East Anglia
> >
> > ______________________________________________
> > R-help at 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.
> >
> 
> 
> 
> -- 
> "Men by nature long to get on to the ultimate truths, and will often
> be impatient with elementary studies or fight shy of them. If it were
> possible to reach the ultimate truths without the elementary studies
> usually prefixed to them, these would not be preparatory studies but
> superfluous diversions."
> 
> -- Maimonides (1135-1204)
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list