Looping grepl() through data.table (R)

后端 未结 3 1581
梦毁少年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:24

    Data.table is good at grouped operations; I think that's how it can help, assuming you have many rows with the same industry:

    DT[ DT[, .I[grep(industry, category)], by = industry]$V1 ]
    

    This uses the current idiom for subsetting by group, thanks to @eddi .


    Comments. These might help further:

    • If you have many rows with the same industry-category combo, try by=.(industry,category).

    • Try something else in the place of grep (like the options in Ken and Richard's answers).

提交回复
热议问题