README.md

Reformating the function total_price()

The function is in the folder R

Installation

You can install the development version of terribleCode from GitHub with:

# install.packages("devtools")
devtools::install_github("svalvaro/terribleCode")

Example

library(terribleCode)
## basic example code

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    price_USD = c(249,591,319,198,125)
    )

# 20 % discount for EUR
total_price(df, currency = 'EUR')
#> [1] 1036.8
(217+517+279+173+110)*0.8
#> [1] 1036.8

Improvements

Missing values

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,NA,' ',173,110),
    price_USD = c(249,591,319,198,125)
    )

total_price(df, currency = 'EUR') # 20% discount
#> [1] 400
(217+173+110)*0.8
#> [1] 400

Prices wrongly read as strings

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    price_USD = c(249,'591','unknown','NA',125)
    )

total_price(df, currency = 'USD') # 25% discount
#> [1] 723.75
(249+591+125)*0.75
#> [1] 723.75

Other currencies (No discount specified)

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    price_USD = c(249,591,319,198,125),
    price_CZK = c(6122, 14530, 7843, 4868, 3073)
    )


total_price(df, currency = 'CZK')
#> [1] 36436
(6122+14530+7843+4688+3073)
#> [1] 36256

Able to recognise different column names

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    price_USD = c(249,591,319,198,125)
    )

df2 <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    USD_price = c(249,591,319,198,125)
    )

total_price(df, currency = 'USD') == total_price(df2, currency = 'USD')
#> [1] TRUE

Error check if currency not found

df <- data.frame(
    product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
    price_EUR = c(217,517,279,173,110),
    price_USD = c(249,591,319,198,125)
    )

total_price(df, currency = 'CZK')
#> Error in total_price(df, currency = "CZK"): The prices for the provided currency were not found.

Error if the input provided is not a data frame

vector <- c(217,517,279,173,110)
total_price(vector, currency = 'CZK')
#> Error in total_price(vector, currency = "CZK"): Data provided is not a data.frame

Less dependencies

Lastly, I have removed the dependency of the magritrr package by removing the usage of the pipe: %>%. It was unnecessary and some users might not be familiar with it.

Tests

A total of six different tests have been created for this function. They are in the folder tests.



svalvaro/terribleCode documentation built on Feb. 2, 2022, 5:18 p.m.