mercredi 26 août 2020

Dealing with NAs in a loop

I have a loop that is checking to see if the each Zipcode[ i ] equals Zipcode[ j ] where there are some NAs for entries of Zipcode[ j ]. I just need the dummy variable for Urban to take a 1 if Zipcode[ j ] is a match for any of the Zipcodes in list urbanZips.

I've tried

for(i in 1:end){
  for(j in 1:end_1){
    if(urbanZips[i]==data_individual$Zipcode[j]) data_individual$Urban=1
}

}

And I've also tried

for(i in 1:end){
  for(j in 1:end_1){
    if(urbanZips[i]==data_individual$Zipcode[j]){
        data_individual$Urban[j]=1
    } else {
        data_individual$Urban[j]=0
    }
}

}

And for both I'm getting Error in if (urbanZips[i] == data_individual$Zipcode[j]) data_individual$Urban = 1: missing value where TRUE/FALSE needed

I know there are NAs but there are also probably some missing values as well. There are close to a million observations.

Aucun commentaire:

Enregistrer un commentaire