R split string at last whitespace chars using tidyr::separate

前端 未结 2 1990
余生分开走
余生分开走 2021-02-14 08:00

Suppose I have a dataframe like this:

df<-data.frame(a=c(\"AA\",\"BB\"),b=c(\"short string\",\"this is the longer string\"))

I would like to

2条回答
  •  天命终不由人
    2021-02-14 08:55

    We can use extract from tidyr by using the capture groups ((...)). We match zero or more characters (.*) and place it within the parentheses ((.*)), followed by zero or more space (\\s+), followed by the next capture group which includes only characters that are not a space ([^ ]) until the end ($) of the string.

    library(tidyr)
    extract(df, b, into = c('partA', 'partB'), '(.*)\\s+([^ ]+)$')
    #   a              partA  partB
    #1 AA              short string
    #2 BB this is the longer string
    

提交回复
热议问题