[R] eliminating constant variables

jim holtman jholtman at gmail.com
Sun Jul 11 01:55:31 CEST 2010


Is this what you want:

> test <- data.frame(a=runif(10), b=rep(NA, 10), c=rep(3,10), d=runif(10))
> test
           a  b c         d
1  0.3390729 NA 3 0.4346595
2  0.8394404 NA 3 0.7125147
3  0.3466835 NA 3 0.3999944
4  0.3337749 NA 3 0.3253522
5  0.4763512 NA 3 0.7570871
6  0.8921983 NA 3 0.2026923
7  0.8643395 NA 3 0.7111212
8  0.3899895 NA 3 0.1216919
9  0.7773207 NA 3 0.2454885
10 0.9606180 NA 3 0.1433044
> # determine which columns contain all NAs, or the same value
> same <- sapply(test, function(.col){
+     all(is.na(.col))  || all(.col[1L] == .col)
+ })
> same
    a     b     c     d
FALSE  TRUE  TRUE FALSE
> # now remove them
> test <- test[!same]
> test
           a         d
1  0.3390729 0.4346595
2  0.8394404 0.7125147
3  0.3466835 0.3999944
4  0.3337749 0.3253522
5  0.4763512 0.7570871
6  0.8921983 0.2026923
7  0.8643395 0.7111212
8  0.3899895 0.1216919
9  0.7773207 0.2454885
10 0.9606180 0.1433044
>


On Sat, Jul 10, 2010 at 7:45 PM, pdb <philb at philbrierley.com> wrote:
>
> Hi Jim,
>
> Thanks for your response, although I was probably not clear about exactly
> what I want to achieve, please let me see if I can explain a little
> better...
>
> There are certain (unknown) columns in my data that contain either NULL in
> every row, or the same value in every row (eg '1'). These columns are
> useless for modelling as there is no variation in the data.
>
> I need a way to automatically find and delete all these columns (it is not
> rows I want to delete, but the whole column, as in
>
> train$Variablexxx = NULL
>
> where Variablexxx needs to be automatically found.
>
> Thanks in advance,
>
> pdb
> --
> View this message in context: http://r.789695.n4.nabble.com/eliminating-constant-variables-tp2284831p2284853.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list