Looping grepl() through data.table (R)

后端 未结 3 1580
梦毁少年i
梦毁少年i 2020-12-18 01:53

I have a dataset stored as a data.table DT that looks like this:

print(DT)
   category            industry
1: administration      admin
2: nurse         


        
3条回答
  •  盖世英雄少女心
    2020-12-18 02:06

    As long as the match is always based on the start of the category string, then this works just fine:

    dt[substring(category, 1, nchar(industry)) == industry]
    #              category industry
    # 1:     administration    admin
    # 2:           trucking    truck
    # 3:     administration    admin
    # 4:           trucking    truck
    # 5: nurse practitioner    nurse
    

提交回复
热议问题