hrkshr
hrkshr

Reputation: 129

Parse number with weird character in r

I have the following data frame. I want to change these numbers to dbl form.

> df1
# A tibble: 44 x 4
   unit_price total_amount unit_price_2 total_amount_2
   <chr>      <chr>        <chr>        <chr>         
 1 3,500.00   3,500.00     10,000.00    10,000.00     
 2 4.50       2,565.00     14.00        7,980.00      
 3 9.00       234.00       18.00        468.00        
 4 7.50       15,000.00    9.50         19,000.00     
 5 15.00      3,960.00     14.00        3,696.00      
 6 15.00      6,750.00     14.00        6,300.00      
 7 25.00      6,525.00     22.00        5,742.00      
 8 48.00      251,875.20   41.00        215,143.40    
 9 48.00      357,163.20   41.00        305,076.90    
10 55.00      11,000.00    41.00        8,200.00      
# … with 34 more rows

But, one-row has the following "-" character. So I can't get the intended result. How do I deal with this issue?

> df1[12,]
# A tibble: 1 x 4
  unit_price total_amount      unit_price_2 total_amount_2   
  <chr>      <chr>             <chr>        <chr>            
1 1.00       -          997.00 1.00         -          997.00
> df1 %>% mutate_at(1:4, parse_number)
Warning: Problem with `mutate()` input `total_amount`.
ℹ 1 parsing failure.
row col expected            actual
 12  -- a number -          997.00

Upvotes: 0

Views: 156

Answers (1)

MrFlick
MrFlick

Reputation: 206197

The parse_number function has an na= parameter so you can tell it what values to treat as missing (which is presumably what you want to do with "-").

Use

df1 %>% mutate_at(1:4, parse_number, na="-")

Check the ?parse_number help page for other options.

Upvotes: 1

Related Questions