[R] Remove columns from dataframe based on their statistics

Johannes Radinger JRadinger at gmx.at
Thu May 31 16:34:08 CEST 2012


Hi James
Hi Jorge,

Thank you very much!
I like the apply-approach, it seems really quite
simple and I get back the TRUE-FALSE vector which
I can use for indexing the dataframe.
Now there popped the questions if one can implement
any "exeption", like do the selection of
the columns exept for column with name "B".
I have to think about this

/Johannes

-------- Original-Nachricht --------
> Datum: Thu, 31 May 2012 09:20:27 -0500
> Von: J Toll <jctoll at gmail.com>
> An: Johannes Radinger <JRadinger at gmx.at>
> CC: R-help at r-project.org
> Betreff: Re: [R] Remove columns from dataframe based on their statistics

> On Thu, May 31, 2012 at 8:52 AM, J Toll <jctoll at gmail.com> wrote:
> 
> > for (i in seq(ncol(df), 1))
> >  if (length(unique(df[, i])) == 1) {
> >  df[, i] <- NULL
> > }
> 
> Here's a similar method employing a more functional approach:
> 
> df[, apply(df, 2, function(x) length(unique(x)) > 1)]
> 
> 
> James

-- 

Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a



More information about the R-help mailing list