0

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'

Paul
  • 2,850
  • 1
  • 12
  • 37
Tanamachi
  • 3
  • 3
  • Hi and welcome to SO, please make sure to share a reproducible example (see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Screenshots are not good because we cannot use them to copy-paste the data and try to answer the question. – Paul May 04 '22 at 14:41

1 Answers1

0

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()
langtang
  • 22,248
  • 1
  • 12
  • 27