Reputation: 175
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
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
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