title: R - extract all strings matching pattern and create relational table
body: <p>I am looking for a shorter and more pretty solution (possibly in tidyverse) to the following problem. I have a data.frame "data":</p>
  
  <pre><code>  id            string
  1  A 1.001 xxx 123.123
  2  B 23,45 lorem ipsum
  3  C      donald trump
  4  D    ssss 134, 1,45
  </code></pre>
  
  <p>What I wanted to do is to extract all numbers (no matter if the delimiter is "." or "," -> in this case I assume that string "134, 1,45" can be extracted into two numbers: 134 and 1.45) and create a data.frame "output" looking similar to this:</p>
  
  <pre><code>  id  string
  1  A   1.001
  2  A 123.123
  3  B   23.45
  4  C    &lt;NA&gt;
  5  D     134
  6  D    1.45
  </code></pre>
  
  <p>I managed to do this (code below) but the solution is pretty ugly for me also not so efficient (two for-loops). Could someone suggest a better way to do do this (preferably using dplyr)</p>
  
  <pre><code># data
  data &lt;- data.frame(id = c("A", "B", "C", "D"), 
                    string = c("1.001 xxx 123.123", 
                               "23,45 lorem ipsum", 
                               "donald trump", 
                               "ssss 134, 1,45"),
                    stringsAsFactors = FALSE)
  
  # creating empty data.frame                     
  len &lt;- length(unlist(sapply(data$string, function(x) gregexpr("[0-9]+[,|.]?[0-9]*", x))))
  output &lt;- data.frame(id = rep(NA, len), string = rep(NA, len))
  
  # main solution
  start = 0
  
  for(i in 1:dim(data)[1]){
    tmp_len &lt;- length(unlist(gregexpr("[0-9]+[,|.]?[0-9]*", data$string[i])))
    for(j in (start+1):(start+tmp_len)){
      output[j,1] &lt;- data$id[i]
      output[j,2] &lt;- regmatches(data$string[i], gregexpr("[0-9]+[,|.]?[0-9]*", data$string[i]))[[1]][j-start]
    }
    start = start + tmp_len
  }
  
  # further modifications
  output$string &lt;- gsub(",", ".", output$string)
  output$string &lt;- as.numeric(ifelse(substring(output$string, nchar(output$string), nchar(output$string)) == ".",
                                     substring(output$string, 1, nchar(output$string) - 1),
                                     output$string))
  
  output
  </code></pre>