Separating column using separate (tidyr) via dplyr on a first encountered digit

后端 未结 2 558
北恋
北恋 2021-01-11 10:51

I\'m trying to separate a rather messy column into two columns containing period and description. My data resembles the extract below:

set.         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 11:49

    You could also use unglue::unnest() :

    dta <- data.frame(indicator=c("someindicator2001", "someindicator2011",
                                  "some text 20022008", "another indicator 2003"),
                      values = runif(n = 4))
    
    # remotes::install_github("moodymudskipper/unglue")
    library(unglue)
    unglue_unnest(dta, indicator, "{indicator}{=\\s*}{period=\\d*}")
    #>       values         indicator   period
    #> 1 0.43234262     someindicator     2001
    #> 2 0.65890900     someindicator     2011
    #> 3 0.93576805         some text 20022008
    #> 4 0.01934736 another indicator     2003
    

    Created on 2019-09-14 by the reprex package (v0.3.0)

提交回复
热议问题