'Problem trying to get column with more 0´s

im using R and im trying to get the column name of the column that has more 0´s , I have this function:

preguntasMenosRespondidas<-function(){ 

  tablaPreguntas = select(data1, c(8:79))
  //ANSWER HERE
 }
preguntasMenosRespondidas()

and the data frame (tablaPreguntas) look like this (the real data frame has like 13krows):

dataframe here

What i need to do if i need that the function return me '2.3'

r


Solution 1:[1]

Here is one way to get the column that has the maximum number of zeros:

which.max(apply(data1[,8:79], 2, function(col) sum(col==0)))

You can wrap this in a function if you want, but I don't think you need to or want to:

preguntasMenosRespondidas <- function() { 
    tablaPreguntas = select(data1, c(8:79))
    which.max(apply(tablaPreguntas, 2, function(col) sum(col==0)))
  }
preguntasMenosRespondidas()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 langtang