0

My script currently is very messy and I was wondering if there is a better way of doing this;

I am trying to workout the final price of a product. This is the steps I take;

Dataset A has all the products which changes every year, Dataset B has the base price for the products which also changes every year.

  1. In Dataset A, find the product base price by matching the product in Dataset B.
  2. In Dataset A, apply numerous variations to the base price, these adjustments change every year. I am currently manually putting in the variations.
  3. After the variations, this is my final price.

Dataset A has columns product. Dataset B has columns product and base.price.

Variation 1 = base price needs to be adjusted by 10% if it meets a condition. Variation 2 = after variation 1, base price adjusted to be 5% if it meets a condition. Variation 3 = after variation 1 and 2, base price to be adjusted by 8% if its meets a condition.

library(tidyverse)

####creating sample database 
product <- c("pants", "shirt", "boots", "dress")
databaseA<-data.frame(product)

base.price <- c(10, 8, 9,16)
databaseB<-data.frame(product,base.price)

###
datasetAB<-dplyr::left_join(datasetA, datasetB, by = c("product"="product"))

#variation 1
datasetAB<-datasetAB%>%mutate(baseprice1=base.price*1.1)

#variation 2
datasetAB<-datasetAB%>%mutate(baseprice2=baseprice1*1.05)

#variation 3
datasetAB<-datasetAB%>%mutate(baseprice3=baseprice2*1.08)

I am trying to workout if there is a better way of doing this, instead of importing so many datasets and referencing all the different datasets in my code. Because it changes every year, there is just too much datasets.

I am sorry, I don't have enough reputations to show properly.

newbie
  • 21
  • 5
  • 1
    Can you provide a sample of your dataset A and B, may b using `dput(datatsetA)` and `dput(datatsetB)` ? – shafee Jul 11 '22 at 04:15
  • It's much easier to help if you can share sample data and any code you've tried. That saves potential answerers from having to guess what your data looks like and what output you're expecting. – TarJae Jul 11 '22 at 05:05

1 Answers1

0

It would definitely help if your example would contain your further calculations including these conditions. Nevertheless, I dare say you're better off applying a "final_price" function to your products.

If you insist on having all your possible prices available in a data.frame, then I suggest you just add columns to a single data.frame, such that you have columns product, base.price, price.conditionA, etc...

Chris
  • 139
  • 9