Remove string from a vector in R

后端 未结 3 510
名媛妹妹
名媛妹妹 2020-12-22 02:26

I have a vector that looks like

> inecodes
   [1] \"01001\" \"01002\" \"01049\" \"01003\" \"01006\" \"01037\" \"01008\" \"01004\" \"01009\" \"01010\" \"01         


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

    Try this. Match should be much faster

    pos<-which(!is.na(pob[match(sub('^([0-9]+)-.*$','\\1',pob),inecodes)]))
    pob[pos]<-sub('^[0-9]+-(.*)$','\\1',pob[pos])
    

    Please do post the timings if you manage to get this. Match usually solves many computational issues for large data sets lookup. Would like to see if there are any opposite scenarios.

提交回复
热议问题