dimanche 15 mars 2020

R - TRUE/FALSE needed error when using a for loop and if statement together

I have an if statement in a that works when I put the [Tag:if -statement] or in isolation in the console, but when I run the whole chunk of code I get an error:

Error in if (CEOsBefore2011$becameceo[z] == CEOsBefore2011$becameceo[z - : missing value where TRUE/FALSE needed

And here is the code

clean_year <- c(1)
clean_company <-c(1)
clean_total_comp <- c(1)
clean_execid <- c(1)
clean_becameceo <- c(1)

z = 5
for (execid in CEOsBefore2011$execid) {
  z = z + 1
  if(CEOsBefore2011$becameceo[z] == CEOsBefore2011$becameceo[z-5]) { 
      clean_year <- c(clean_year, CEOsBefore2011$year[z:z-5])
      clean_company <- c(clean_company, CEOsBefore2011$Company[z:z-5])
      clean_total_comp <- c(clean_total_comp, CEOsBefore2011$TotalComp[z:z-5])
      clean_execid <- c(clean_execid, CEOsBefore2011$execid[z:z-5])
      clean_becameceo <- c(clean_becameceo, CEOsBefore2011$becameceo[z:z-5])
      print(z)
  } else{
  }
}

Basically what I'm trying to do is to clean up the data by checking each row. I need to have at least 6 rows in a row of the same becameceo date to be included in the data otherwise remove it. Any thoughts?

Aucun commentaire:

Enregistrer un commentaire