How to str_extract percentages in R?

让人想犯罪 __ 提交于 2019-12-10 23:20:00

问题


From this string border-color:#002449;left:74.4%top;37%; I would like to make the first percentage 74.4% a variable called X and the second percentage 37% a variable called Y.

I have tried to play around with this regex "^.*?(\\d+)%.*" but this takes out the % sign and only extracts the second 4 from 74.4

Any help will be appreciated. Please let me know if any further information is needed.


回答1:


s <- "border-color:#002449;left:74.4%top;37%;"
regmatches(s, gregexpr("\\d+(\\.\\d+){0,1}%", s))[[1]]
# [1] "74.4%" "37%"  

or

library(stringr)
str_extract_all(s, "\\d+(\\.\\d+){0,1}%")[[1]]
# [1] "74.4%" "37%"  


来源:https://stackoverflow.com/questions/35822899/how-to-str-extract-percentages-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!