0
    $Budget(chr)"3,000.00", "2,000.00", "2,000.00"...  
    $Impressions(int) 0, 0, 0, 0...  
    $Interactions(int) 0, 0, 0...  
    $Interaction.Rate(chr) "0.00%", "0.00%", "0.00%"...  
    $Avg..Cost (dbl) 0, 0, 0, 0, 0, 0, 0...  
    $Cost(chr) "0", "0", "0", "0"...  
    $Clicks(int) 0, 0, 0, 0...  
    $CTR(chr) "0.00%", "0.00%", "0.00%"...  

I have some columns.All of them have numeric values but when I imported them in R they appeared to be of a character data type.I want to convert these character to integer and also remove % sign from the columns that have them.

    mData=mData %>%
    mutate(Budget=as.numeric(Budget),  
    Interaction.Rate=as.numeric(gsub("%","",Interaction.Rate)),  
    Cost=as.numeric(Cost),  
    CTR=as.numeric(gsub("%","",CTR)))  

I tried doing this but no result instead it gives me error:

Warning message: In eval(substitute(expr), envir, enclos) : NAs introduced by coercion

Can anyone help me out with this

Jaap
  • 81,064
  • 34
  • 182
  • 193
richa1465
  • 23
  • 6
  • 3
    Possible duplicate of [How to convert a data frame column to numeric type?](http://stackoverflow.com/questions/2288485/how-to-convert-a-data-frame-column-to-numeric-type) – Sathish Mar 24 '17 at 11:03
  • 1
    You need to remove the comma from the `Budget`-column as well with `gsub`. Another possibility is to use the `parse_number`-function from the `readr`-package. – Jaap Mar 24 '17 at 11:08

1 Answers1

0

You can use transform to convert them back to numeric.

transform(mData, Budget= as.numeric(Budget), Interaction.Rate = as.numeric(nteraction.Rate), ...)

To remove , and % use gsub (got the idea from here)

 mData$Budget<- as.numeric(gsub("," ,"" ,mData$Budget))
Community
  • 1
  • 1
Fadwa
  • 1,717
  • 5
  • 26
  • 43