title: How to extract a number from a string in a dataframe and place it in a new column?
body: <p>I have a simple dataframe:</p>
  
  <pre><code>df &lt;- data.frame(test = c("test_A_1_1.txt", "test_A_2_1.txt", "test_A_3_1.txt"), value = c(0.51, 0.52, 0.56))
  
            test   value
  1 test_A_1_1.txt  0.51
  2 test_A_2_1.txt  0.52
  3 test_A_3_1.txt  0.56
  </code></pre>
  
  <p><strong>Expected output</strong></p>
  
  <p>I would like to copy the numbers on the end of the string in column 1 and placed it in column three or four respectively, like this:</p>
  
  <pre><code>          test value  new new
  1 test_A_1.txt  0.51   1  1
  2 test_A_2.txt  0.52   2  1
  3 test_A_3.txt  0.56   3  1
  </code></pre>
  
  <p><strong>Attempt</strong></p>
  
  <p>Using the following code, I am able to extract the numbers from the string:</p>
  
  <pre><code>library(stringr)
  as.numeric(str_extract_all("test_A_3.txt", "[0-9]+")[[1]])[1] # Extracts the first number
  as.numeric(str_extract_all("test_A_3.txt", "[0-9]+")[[1]])[2] # Extracts the second number
  </code></pre>
  
  <p>I would like to apply this code on all the values of the first column:</p>
  
  <pre><code>library(tidyverse)
  df %&gt;% mutate(new = as.numeric(str_extract_all(df$test, "[0-9]+")[[1]])[1])
  </code></pre>
  
  <p>However, this lead to a column <code>new</code>, with only the number <code>1</code>. 
  What am I doing wrong?</p>