mardi 1 septembre 2020

Replace elements of a character vector based on string in R

So I have a dataframe like so:

Occupation
ELECTRICIAN
ROAD ELECTRICIAN
ELECTRICIAN
FARMER
GRASS ELECTRICIAN
POLE ELECTRICIANS
ELECTRICIAN
INSPECTOR

So I would like any time ELECTRICIAN appears to be just ELECTRICIAN, no matter anything else in the cell.

So final product should be:

Occupation
ELECTRICIAN
ELECTRICIAN
ELECTRICIAN
FARMER
ELECTRICIAN
ELECTRICIAN
ELECTRICIAN
INSPECTOR

I tried the following but this did not work....

ifelse(grep('CONDUCTOR',df$Occupation , value=TRUE), "CONDUCTOR",df$Occupation) 

Aucun commentaire:

Enregistrer un commentaire