Extracting numbers from vectors of strings

前端 未结 11 1282
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:05

I have string like this:

years<-c(\"20 years old\", \"1 years old\")

I would like to grep only the numeric number from this vector. Expe

11条回答
  •  一整个雨季
    2020-11-22 04:38

    Using the package unglue we can do :

    # install.packages("unglue")
    library(unglue)
    
    years<-c("20 years old", "1 years old")
    unglue_vec(years, "{x} years old", convert = TRUE)
    #> [1] 20  1
    

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

    More info: https://github.com/moodymudskipper/unglue/blob/master/README.md

提交回复
热议问题