regex - ifelse pattern matching in r -
i fill in new column 1 of 2 values if pattern matched.
here data frame:
df <- structure(list(loc_01 = c("apis", "indu", "isro", "miss", "non_apis", "non_indu", "non_isro", "non_miss", "non_piro", "non_sacn", "non_slbe", "non_voya", "piro", "sacn", "slbe", "voya"), loc01_land = c(165730500, 62101800, 540687600, 161140500, 1694590200, 1459707300, 1025051400, 1419866100, 2037064500, 2204629200, 1918840500, 886299300, 264726000, 321003900, 241292700, 530532000)), class = "data.frame", row.names = c(na, -16l), .names = c("loc_01", "loc01_land")) and looks this...
loc_01 loc01_land 1 apis 165730500 2 indu 62101800 3 isro 540687600 4 miss 161140500 5 non_apis 1694590200 6 non_indu 1459707300 7 non_isro 1025051400 8 non_miss 1419866100 9 non_piro 2037064500 10 non_sacn 2204629200 11 non_slbe 1918840500 12 non_voya 886299300 13 piro 264726000 14 sacn 321003900 15 slbe 241292700 16 voya 530532000 i add column df, called 'loc_01'. if loc_01 contains non, return 'outside', if not contain non, return 'inside'. ifelse statement, i'm missing because returns false value.
df$loc01 <- ifelse(df$loc_01=="non",'outside','inside') and resulting df...
loc_01 loc01_land loc01 1 apis 165730500 inside 2 indu 62101800 inside 3 isro 540687600 inside 4 miss 161140500 inside 5 non_apis 1694590200 inside 6 non_indu 1459707300 inside 7 non_isro 1025051400 inside 8 non_miss 1419866100 inside 9 non_piro 2037064500 inside 10 non_sacn 2204629200 inside 11 non_slbe 1918840500 inside 12 non_voya 886299300 inside 13 piro 264726000 inside 14 sacn 321003900 inside 15 slbe 241292700 inside 16 voya 530532000 inside thanks -al
to check if string contains substring, can't use == because performs exact matching (i.e. returns true if string "non").
use example grepl function (belonging grep family of functions) performs pattern matching:
df$loc01 <- ifelse(grepl("non",df$loc_01),'outside','inside') result :
> df loc_01 loc01_land loc01 1 apis 165730500 inside 2 indu 62101800 inside 3 isro 540687600 inside 4 miss 161140500 inside 5 non_apis 1694590200 outside 6 non_indu 1459707300 outside 7 non_isro 1025051400 outside 8 non_miss 1419866100 outside 9 non_piro 2037064500 outside 10 non_sacn 2204629200 outside 11 non_slbe 1918840500 outside 12 non_voya 886299300 outside 13 piro 264726000 inside 14 sacn 321003900 inside 15 slbe 241292700 inside 16 voya 530532000 inside
Comments
Post a Comment