Home > Net >  R - if field A is empty, and field B contains certain partial string, add word to field A
R - if field A is empty, and field B contains certain partial string, add word to field A

Time:10-20

I have a dataframe with one field that contains geographic names or phrases (locality.curated), and a corresponding field with an abbreviated code for these regions (convert), however not all the abbreviations are filled, some are NA. I want to add abbreviations to the convert field by matching to a key word in locality.curated, but not overwrite the convert field if it is already occupied.

ie: if convert = NA AND locality curated = "Delaware" then paste "USA" to curated.

I have tried various permutations of ifelse and if but either cannot get the syntax right, or, like my latest attempt below, doesn't seem to modify the dataframe.

if(is.na(test$convert) && grepl("Delaware", test$locality.curated, value=T)) {paste("USA", test$convert)}

Dummy data, input:

test <- data.frame(locality.curated=c("Canada to Delaware River", "California", "Delaware", "Wilmington Delaware", "Alaska"), convert=c("CAN","USA", "USA",NA,NA))

desired output:

test.out <- data.frame(locality.curated=c("Canada to Delaware River", "California", "Delaware", "Wilmington Delaware", "Alaska"), convert=c("CAN","USA", "USA","USA",NA))

Many thanks!

CodePudding user response:

test$new <- ifelse(is.na(test$convert) & grepl(pattern = "Delaware", x = test$locality.curated),
       yes = "USA",
       no = test$convert)
1 Canada to Delaware River     CAN  CAN
2               California     USA  USA
3                 Delaware     USA  USA
4      Wilmington Delaware    <NA>  USA
5                   Alaska    <NA> <NA>

  • Related