rdataframeunique-values

Remove columns with same value from a dataframe


I've got a data frame like this one

1    1    1    K    1    K    K
2    1    2    K    1    K    K
3    8    3    K    1    K    K
4    8    2    K    1    K    K
1    1    1    K    1    K    K
2    1    2    K    1    K    K

I want to remove all the columns with the same value, i.e K, so my result will be like this

1    1    1    1    
2    1    2    1   
3    8    3    1  
4    8    2    1  
1    1    1    1 
2    1    2    1  

I try to iterate in a for by columns but I didn't get anything. Any ideas?


Solution

  • To select columns with more than one value regardless of type:

    uniquelength <- sapply(d,function(x) length(unique(x)))
    d <- subset(d, select=uniquelength>1)
    

    ?

    (Oops, Roman's question is right -- this could knock out your column 5 as well)

    Maybe (edit: thanks to comments!)

    isfac <- sapply(d,inherits,"factor")
    d <- subset(d,select=!isfac | uniquelength>1)
    

    or

    d <- d[,!isfac | uniquelength>1]