Convert monetary values "R $" to double type

0

Hello, I have several CSV files, in which some monetary values have been stored in real columns: "R $ 1,986.00". However, I want to pass all CSVs to SQLite and then parse the data in Power BI. So, instead of exporting the CSV data to SQLite in the character type, I would like to export them as double type (I think this would be the appropriate option, I believe), in order to facilitate the process of data processing in Power BI.

In this context, I would like to know if there is any function or package in R that can convert data from a data.frame (imported from a CSV) that is in the currency format into real ("$ 1,986.69") for type double ("1986.69")?

Following is the example of the columns that are stored in the CSV. In this case, there will be about 27,000 CSV files, with an estimate of 20 million lines that will be consolidated in SQLite.

Thanksinadvanceforyoursupport.

Afterthehelpof@RuiBarradas,Ididacomplementationintheregularexpressiontogetseveralsituations.Itwasthus,asanexample:

    
asked by anonymous 28.11.2017 / 22:17

1 answer

3

Try to apply the converte function to each of the columns with the problematic values.

converte <- function(x) as.numeric(sub(",", "\.", gsub("R\$|\.", "", x)))

x <- "R$ 5.500,00"
converte(x)
#[1] 5500

As the sub and gsub functions are vectorized, converte is also functions and processes entire columns.

    
28.11.2017 / 23:36