Extract text after “/” in a data frame column

前端 未结 1 903
广开言路
广开言路 2020-11-30 15:39

I have a data frame that has two columns Link and Value. The Link column has values like \"abcd.com/efgh/ijkl/mnop\" and is a URL. The

相关标签:
1条回答
  • 2020-11-30 16:22

    Try basename(). It

    removes all of the path up to and including the last path separator (if any).

    basename("abcd.com/efgh/ijkl/mnop")
    # [1] "mnop"
    

    It is vectorized, so you can just stick the whole column in there.

    basename(rep("abcd.com/efgh/ijkl/mnop", 3))
    # [1] "mnop" "mnop" "mnop"
    

    So, to apply this to one column link of a data frame webdata, you can simply do

    webdata$link <- basename(webdata$link)
    

    The other obvious function would be sub(), but I think basename() will do the trick and it's easier.

    sub(".*/", "", rep("abcd.com/efgh/ijkl/mnop", 3))
    
    0 讨论(0)
提交回复
热议问题