Home > Back-end >  create new column based on existing pattern R
create new column based on existing pattern R

Time:03-08

I have this

tableRules <- as.data.table(data.frame(object_name = c("instr_asset_row","functional_cat","ref_sector_second")))

listDimPoss <- c("instr_asset","ref_sector","functional_cat")

I want to create a new column in tableRules: tableRules$DIMENSION that will check in tablerules$object_name if the value includes the patterns listed in listdimPoss. If yes, then I want the value of tableRules$Rimension to be the pattern listed in listDimPoss. so here I want as a results

tableRules$DIMENSION <- c("instr_asset","functional_cat","ref_sector")))

CodePudding user response:

We may use regex_left_join

library(data.table)
library(fuzzyjoin)
regex_left_join(tableRules,  data.table(DIMENSION = listDimPoss), 
     by = c("object_name" = "DIMENSION"))
        object_name      DIMENSION
1   instr_asset_row    instr_asset
2    functional_cat functional_cat
3 ref_sector_second     ref_sector
  • Related