Post

DP08 Apply changes to Group of Fields using RegEx

Description

Financial data often arrives in a text-based format, with dollar signs and commas incorporated for ease of reading. However, numerical analyses and computations are more effective when the data is presented in numeric format.

This script focuses on addressing a common data transformation task involving currency values. It applies a regular expression (RegEx), defined within a dedicated function, to convert currency representations within a text (e.g., “$0,000”) into a numeric format.

R Script - Apply changes to Cost fields

Original Data with Cost Fields as Characters

The next graph shows the original data containing cost fields in the format “$0,000.00”:

08 Results Structure of Cost Fields as Characters

Function to Change the Format of Cost as character to numeric

Using Regular Expression to remove the $ signs and the commas. Then converting to numeric format.

1
fx_convmoney <- function(x){as.numeric(gsub("[\\$,]", "", x))}

Transformed Data

The result of applying the script is the transformation of currency-based cost fields from their initial character format to a more analytically useful numeric representation.

08 Results Structure of Cost Fields after the Change of Format

__

End of Post

This post is licensed under CC BY 4.0 by the author.