Drop columns in a data.frame with conditions R

I am trying to be lazier than ever with R and I was wondering to know if there is a chance to drop columns from a data.frame by using a condition.

For instance, let's say my data.frame has 50 columns.

I want to drop all the columns that share each other

mean(mydata$coli)... = mean(mydata$coln) = 0

How would you write this code in order to drop them all at once? Because I use to drop columns with

mydata2 <- subset(mydata, select = c(vari, ..., varn))

Obviously not interesting because of the need of manual data checking.

Thank you all!

Upvotes: 1

Views: 1321

Answers (2)

akrun
akrun

Reputation: 887008

We can use colMeans to get the mean of all the columns as a vector, convert that to a logical index (!=0) and subset the dataset.

mydata[colMeans(mydata)!=0]

Or use Filter with f as mean. If the mean of a column is 0, it will be coerced to FALSE and all others as TRUE to filter out the columns.

Filter(mean, mydata)

data

mydata <- data.frame(col1=0, col2=1:7, col3=0, col4=-3:3)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388862

Something similar as @akrun using lapply

mydata <- data.frame(col1=0, col2=1:7, col3=0, col4=-3:3)
mydata[lapply(mydata, mean)!=0]

# col2
#1    1
#2    2
#3    3
#4    4
#5    5
#6    6
#7    7

Upvotes: 2

Related Questions