提取數字我有一系列的字符串如下:與單位從字符串
x <- " 20 to 80% of the sward should be between 3 and 10cm tall,
with 20 to 80% of the sward between 10 and 30cm tall"
我想提取的數值,並保持了單位,我已經試過如下:
x <- lapply(x, function(x){gsub("[^\\d |cm\\b |mm\\b |% ]", "", x, perl = T)})
這給:
" 20 80% 3 10cm 20 80% 10 30cm "
我需要的是:
"20 80%" "3 10cm" "20 80%" "10 30cm"
感謝您閱讀
將總是存在範圍之間的'和'或一個'to'? – hrbrmstr
嘗試庫(stringr); do.call(rbind,lapply(str_extract_all(x,'\\ d +(\\ s + | cm \\ b |%)'),function(x){m1 < - matrix(x ,ncol = 2,byrow = TRUE); paste(m1 [,1],m1 [,2])}))' – akrun