samedi 3 juillet 2021

Loops in a dataframe- R

I have a dataframe that looks like this (but for every US county)

Countyname Neighbour County Neighbour State
Autauga County, AL Chilton County AL
Autauga County, AL Dallas County AL
Baldwin County, AL Escambia County FL
Catron County, NM Apache County AZ

For all counties, if the neighbouring county is in the same state I want to replace the value in the Neighbour State column with a missing value NA, and if it's in a different state I want to keep it unchanged. I.e. I want to end up with something like this:

Countyname Neighbour County Neighbour State
Autauga County, AL Chilton County NA
Autauga County, AL Dallas County NA
Baldwin County, AL Escambia County FL
Catron County, NM Apache County AZ

I was thinking of looping through each row and if the column 'Countyname' contains the entry in the column 'Neighbour State', replace the entry with NA (e.g. for the first row if 'Autauga County, AL' contains 'AL', replace 'AL' with NA). How would I go about this (or is there a more efficient way to do it as this feels clunky)?

Aucun commentaire:

Enregistrer un commentaire