title: Remove commas from character vectors based on specific column names in R
body: <p>I have a large dataframe.  A smaller subset is as follows:</p>
  
  <pre><code>structure(list(Date = c("2017-08-12", "2017-08-12", "2017-08-12"
    ), `Time (sec)` = c("19:01:04", "07:30:18", "04:29:38"), `4+DURATION` = c("26", 
    "58,000", "27"), `4+'000 (AVG)` = c("0.0000", "0.0000", "0.0000"), 
    `15+DURATION` = c("26", "57,000", "27"), `15+'000 (AVG)` = c("0.0000", 
    "0.0000", "0.0000")), .Names = c("Date", "Time (sec)", "4+DURATION", 
     "4+'000 (AVG)", "15+DURATION", "15+'000 (AVG)"), row.names = 3:5, class = "data.frame")
  </code></pre>
  
  <p>The actual data frame looks like this:</p>
  
  <pre><code>       Date Time (sec) 4+DURATION 4+'000 (AVG) 15+DURATION 15+'000 (AVG)
  3 2017-08-12   19:01:04         26       0.0000          26        0.0000
  4 2017-08-12   07:30:18     58,000       0.0000      57,000        0.0000
  5 2017-08-12   04:29:38         27       0.0000          27        0.0000
  </code></pre>
  
  <p>In this from column 3 onwards the rest of the columns were stored as character vector.  I am trying to convert the character to numeric.  The following is the code that I used.</p>
  
  <pre><code>cols.num &lt;- names(dat[,-c(1:2)])
  dat[cols.num] &lt;- sapply(dat[cols.num],as.numeric)
  </code></pre>
  
  <p>dat is my data frame.  This coerces NA values in both the duration columns where the character value has a extra comma in it.</p>
  
  <p>I tried to remove it by </p>
  
  <pre><code>df[,unique(grep("DUR", names(df), value=T))] &lt;- gsub(",","",df[,unique(grep("DUR", names(df), value=T))])
  </code></pre>
  
  <p>But this creates a df like this</p>
  
  <pre><code>    Date Time (sec)           4+DURATION 4+'000 (AVG)          15+DURATION 15+'000 (AVG)
  3 2017-08-12   19:01:04 c("26" "58000" "27")       0.0000 c("26" "57000" "27")        0.0000
  4 2017-08-12   07:30:18 c("26" "57000" "27")       0.0000 c("26" "58000" "27")        0.0000
  5 2017-08-12   04:29:38 c("26" "58000" "27")       0.0000 c("26" "57000" "27")        0.0000
  </code></pre>
  
  <p>But the desired output is:</p>
  
  <pre><code>   Date Time (sec) 4+DURATION 4+'000 (AVG) 15+DURATION 15+'000 (AVG)
  3 2017-08-12   19:01:04         26       0.0000          26        0.0000
  4 2017-08-12   07:30:18      58000       0.0000        57000        0.0000
  5 2017-08-12   04:29:38         27       0.0000          27        0.0000
  </code></pre>
  
  <p>The problem in this data frame is, I don't know which column will have the duration value and the column name with duration value keeps changing, from 4+DURATION to 45+DURATION, etc.  I want to remove the comma from all the vectors with DURATION in their names before sapplying the vector to numeric.</p>