title: Converting a currency string to a numeric in R
body: <p>Dear StackOverflow community,
  I am working right now on a way to convert a vector of currency data stored as a character into a numeric vector with the ability to transform the currency into another currency.</p>
  
  <p>So imagine my vector is:</p>
  
  <pre><code>x &lt;- c("$5M", "€10B", "CHF5K")
  </code></pre>
  
  <p>and I would like to transform it into billion USD, so the result should be (random currency rates considered):</p>
  
  <pre><code>x &lt;- c(5,11000,0.4)
  </code></pre>
  
  <p>I developed a solution for this with the creation of two functions, my first function removes the thousands, billions and millions characters and transforms it:</p>
  
  <pre><code>convMK &lt;- function(cats){
  for(i in 1:length(cats)){
    if(grepl("M",cats[i])==TRUE){
      cats[i] &lt;- gsub("M","",cats[i])
    } else if(grepl("K",cats[i])==TRUE){
      temp &lt;- "0."
      cats[i] &lt;- gsub("K","",cats[i])
      cats[i] &lt;-paste0(temp,cats[i])
    } else if(grepl("B",cats[i])==TRUE){
      temp &lt;- "00"
      cats[i] &lt;- gsub("B","",cats[i])
      cats[i] &lt;-paste0(cats[i],temp)
      cats[i] &lt;- gsub("\\.","",cats[i])
    } else{}
  }
    return(cats)
  }
  </code></pre>
  
  <p>The second one transforms it into a numeric considering the exchange rates:</p>
  
  <pre><code>convCurr2 &lt;- function(cats) {
    catsNum &lt;- c(0)
    for (i in 1:length(cats)) {
      if (grepl("\\$", cats[i]) == TRUE) {
        cats[i] &lt;- gsub("\\$", "", cats[i])
        catsNum[i] &lt;- as.numeric(cats[i])
        catsNum[i] &lt;- catsNum[i] * exUSD
      } else if (grepl("\\€", cats[i]) == TRUE) {
        cats[i] &lt;- gsub("\\€", "", cats[i])
        catsNum[i] &lt;- as.numeric(cats[i])
        catsNum[i] &lt;- catsNum[i] * exEUR
      } else if (grepl("CA", cats[i]) == TRUE) {
        cats[i] &lt;- gsub("CA", "", cats[i])
        catsNum[i] &lt;- as.numeric(cats[i])
        catsNum[i] &lt;- catsNum[i] * exCA
      } else if (grepl("\\£", cats[i]) == TRUE) {
        cats[i] &lt;- gsub("£", "", cats[i])
        catsNum[i] &lt;- as.numeric(cats[i])
        catsNum[i] &lt;- catsNum[i] * exGBP
      } else if (grepl("\\CHF", cats[i]) == TRUE) {
        cats[i] &lt;- gsub("CHF", "", cats[i])
        catsNum[i] &lt;- as.numeric(cats[i])
        catsNum[i] &lt;- catsNum[i] * exCHF
      }
    }
    return(catsNum)
  }
  </code></pre>
  
  <p>And then I would run the functions in that order:</p>
  
  <pre><code>cats&lt;-convMK(cats)
  cats &lt;- convCurr2(cats)
  </code></pre>
  
  <p>My question now is: Isn't there an easier, shorter way? Because this seems to be way too complex! Especially because I still haven't implemented a solution to the problem, that the function also should look up the correct exchange rate to a given date.</p>
  
  <p>I am very curious for your answers, I only started learning R one week ago and as a marketing student I don't have that much coding experience (meaning: none). Thus, I am eager learning to write more elegant code:)</p>