I have this data frame (train) where I have 2314 variables and I want to drop the columns where the unique value is 0.
Example:
So, I want to remove all the columns with 0 unique values.
> unique(train$makeAm.General)
[1] 0
And, only keep the columns where the unique value is 0 and 1
> unique(train$makeAston.Martin)
[1] 0 1
So, basically you want to remove all columns that are all zero? Is that it?
I don't have a data to test with, but something like this will do it:
train[colSums(train)>0]
Edit: (in reply to #3)
@TalhaAsif that's not what your main thread says. It talks about columns with just 0's.
For your changed requirement, try something like this.
train[lapply(train, function(x) length(unique(x))) > 1]
1 Like
No, I just want to keep all the columns with length 2. Like this:
length(unique(train$makeAcura))
[1] 2
And delete columns with:
length(unique(train$makeAm.General))
[1] 1
please see the next reply
library(tidyverse)
#(train <- head(iris) %>% mutate(exampl0 = 0))
select(train,
where(~length(unique(.))>1))
system
Closed
7
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.