[R] delete columns which partially match expression

William Dunlap wdunlap at tibco.com
Thu Oct 13 17:34:10 CEST 2011


Be careful with the idiom
   x[, -which(columnIsBad)]
If no columns are bad this leads to
   x[, -integer(0)]
which is a data.rame with no columns,
exactly the opposite of what you want.
   x[, !columnIsBad]
doesn't have that problem.  However, if
you can't tell if a column is bad or not
(i.e., columnIsBad contains an NA) you
will have to process columnIsBad to turn
that NA into a definite TRUE or FALSE.

Finally, add the drop=FALSE argument to []
in case the result would be a one-column
data.frame to prevent it from being converted
to the column itself.

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 jim holtman
> Sent: Thursday, October 13, 2011 6:24 AM
> To: Samir Benzerfa
> Cc: r-help at r-project.org
> Subject: Re: [R] delete columns which partially match expression
> 
> try this:
> 
> > x <- read.table(textConnection("A             B             C             D             E
> + 12           33           Error1   71           Error2
> + 12           33           Error1   71           Error2
> + 12           33           Error1   71           Error2
> + 12           33           Error1   71           Error2
> + 12           33           Error1   71           Error2")
> +     , as.is = TRUE
> +     , header = TRUE
> +     )
> > closeAllConnections()
> > colMatch <- which(apply(x, 2, function(a) any(grepl("Error", a))))
> > colMatch
> C E
> 3 5
> > # delete columns
> > x[, -colMatch]
>    A  B  D
> 1 12 33 71
> 2 12 33 71
> 3 12 33 71
> 4 12 33 71
> 5 12 33 71
> >
> 
> 
> On Thu, Oct 13, 2011 at 9:10 AM, Samir Benzerfa <benzerfa at gmx.ch> wrote:
> > Hello everyone,
> >
> >
> >
> > I'd like to search for certain "expressions" (characters) in my data.frame
> > and delete the containing columns.
> >
> >
> >
> > So, for example in the below table, I'd like to delete all columns which
> > contain the expression "Error". That is, R should delete column C and E from
> > my data. Any ideas?
> >
> >
> >
> > A             B             C             D             E
> >
> > 12           33           Error1   71           Error2
> >
> >
> >
> > Cheers, S.B.
> >
> >
> >
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> --
> Jim Holtman
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> 
> ______________________________________________
> 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