Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove columns that have only a unique value

Tags:

r

I want to remove columns that have only a unique value.

First, I try it for a single column and it works:

data %/% 
  select_if(length(unique(data$policy_id)) > 1)

then I try it for multiple columns as below:

data %/% 
  select_if(length(unique(data[, c("policy_date", "policy_id"])) > 1)

but it does not work. I think it is a conceptual mistake due to my lack of experience.

thanks in advance

like image 586
senad Avatar asked Oct 27 '20 13:10

senad


2 Answers

You can use select(where()).

Suppose I have a data frame like this:

df <- data.frame(A = LETTERS[1:5], B = 1:5, C = 2)

df
#>   A B C
#> 1 A 1 2
#> 2 B 2 2
#> 3 C 3 2
#> 4 D 4 2
#> 5 E 5 2

Then I can do:

df %>% select(where(~ n_distinct(.) > 1))

#>   A B
#> 1 A 1
#> 2 B 2
#> 3 C 3
#> 4 D 4
#> 5 E 5
like image 57
Allan Cameron Avatar answered Oct 22 '22 11:10

Allan Cameron


Some base R options:

  • Using lengths + unique + sapply
subset(df,select = lengths(sapply(df,unique))>1)
  • Using Filter + length + unique
Filter(function(x) length(unique(x))>1,df)
like image 37
ThomasIsCoding Avatar answered Oct 22 '22 10:10

ThomasIsCoding