Ifelse does not work, any ideas?

0

I have this function to process a database that I am using, the issue is that the result only classifies everything as "public" and does not seem to detect the other condition, any suggestions?

sectorlaboral = function (funcionariopublicoinstitucion) {
  if(becal_completo$funcionariopublicoinstitucion!="NO")
    return("publico")
  else if(becal_completo$funcionariopublicoinstitucion=="NO")
    return("privado")
}

The variable is "belonging to a public institution" so the answers are either "no" or the name of the institution. Since I have to calculate the number of people belonging to the private sector and the public, I am creating a new column that classifies those not as private and the others as public.

    
asked by Jazz Duarte 13.02.2018 в 21:04
source

2 answers

1

Eventually you do not need to make a new function for what you are looking for, you can use the base function ifelse() as follows:

becal_completo$Institucion <- ifelse(becal_completo$funcionariopublicoinstitucion == "NO", "privado", "publico")
    
answered by 14.02.2018 / 00:13
source
0

I found the solution, in case it serves someone, the problem was as indicated by my variable within the function, it was not necessary to refer to the dataframe with $:

sectorlaboral = function (funcionariopublicoinstitucion) { 

  if (funcionariopublicoinstitucion=="NO" | funcionariopublicoinstitucion=="NO ")

    return ("privado")

 else 

    return ("publico")
}
    
answered by 13.02.2018 в 23:52