Split Character String Using Only Last Delimiter in r

后端 未结 4 879
暖寄归人
暖寄归人 2021-01-16 07:38

I have a character variable that I would like to split into 2 variables based on a \"-\" delimiter, however, I would only like to split based on the last delimiter as there

4条回答
  •  执笔经年
    2021-01-16 08:11

    Using unglue you would do :

    # install.packages("unglue")
    library(unglue)
    df <- data.frame(input = c("foo - bar","hey-now-man","say-now-girl","fine-now"))
    unglue_unnest(df, input, "{output1}{=\\s*-\\s*}{output2=[^-]+}", remove = FALSE)
    #>          input output1 output2
    #> 1    foo - bar     foo     bar
    #> 2  hey-now-man hey-now     man
    #> 3 say-now-girl say-now    girl
    #> 4     fine-now    fine     now
    

    Created on 2019-11-06 by the reprex package (v0.3.0)

提交回复
热议问题