Ayechan_San
Ayechan_San

Reputation: 175

Remove columns with certain number of zeros - R

How can I remove columns with more than 6 zeros in my data?

Please let me know how I can make my sample code achieve what I need to do, and if you have a shorter way, please let me know that as well. Explaining and teaching me why it works would also be appreciated.

My sample code to remove zeros:

removeThese = c()
for(i in 1:ncol(myData))
{
rowsWithZeros = which(myData[,i] == 0)
if(length(rowsWithZeros) > 6)
{
removeThese = c(removeThese, i)
}
}
myData= myData[,-removeThese]

Upvotes: 0

Views: 995

Answers (3)

bramtayl
bramtayl

Reputation: 4024

If you have a dataframe:

Filter(function(x) sum(x==0)<=6, df)

Upvotes: 0

Robert Hijmans
Robert Hijmans

Reputation: 47061

How about

i <- colSums(myData == 0, na.rm=TRUE) < 7
myData <- myData[, i, drop=FALSE]

or, following Richard

i <- colSums(myData == 0, na.rm=TRUE) < 7
myData <- myData[i]

Upvotes: 2

mateen
mateen

Reputation: 83

If you want to shorten the number of lines, replacing for loop with an appropriate *apply statement works well.

sixZeroes = function(col) {
  sum(col==0)>6
}

myData=myData[,apply(myData,2,sixZeroes)]

The function can be inline, if you want a one-liner.

Upvotes: 0

Related Questions