library(tidyverse)
library(dplyr)
# Input test data
Component2 <- data.frame(
stringsAsFactors = FALSE,
Component_Product_Code = c("Component6",
"Component7","Component8","Component9","Component22"),
Quantity = c(1L, 2L, 4L, 28L, 90L),
Wastage_Quantity = c(0L, 0L, 0L, 0L, 0L),
Unit_Cost = c(127.2, 112.02, 23.33, 0.0342, 0.0453),
Total_Cost = c(127.2, 224.04, 93.32, 0.9576, 4.077),
Can_Auto_Assemble = c("Yes", "Yes", "Yes", "Yes", "Yes"),
Can_Auto_Disassemble = c("No", "No", "No", "No", "No"),
IsObsoleted = c("No", "No", "No", "No", "No"),
Expense_Account = c(NA, NA, NA, 51120L, 51120L),
Comments = c(NA, NA, NA, NA, NA)
)
Component9 <- data.frame(
stringsAsFactors = FALSE,
Component_Product_Code = c("Component11",
"Component12","Component13","Component14",
"Component15"),
Quantity = c(5L, 5L, 2L, 1L, 1L),
Wastage_Quantity = c(0L, 0L, 0L, 0L, 0L),
Unit_Cost = c(107, 53, 49.3629, 55.3854, 134.186),
Total_Cost = c(535, 265, 98.7258, 55.3854, 134.186),
Can_Auto_Assemble = c("Yes", "Yes", "Yes", "Yes", "Yes"),
Can_Auto_Disassemble = c("No", "No", "No", "No", "No"),
IsObsoleted = c("No", "No", "No", "No", "No"),
Expense_Account = c(51250L, 51244L, NA, NA, NA),
Comments = c(NA, NA, NA, NA, NA)
)
Updated_price_list <- data.frame(
stringsAsFactors = FALSE,
Assembled_Product_Code = c("Component14", "Component23"),
Total_Cost = c(12.5396, 9.9738)
)
# I have many thousands of data.frames. Here, I have given two examples called Component2 and Component9.
# Each data frame has a column called Total_Cost that specifies costs of components that it is made of.
# For example, dataframe Component2 consists of Components6, 7, 8, 9 and 22 where the cost of each component is
# found in the Total_Cost column.
# I need to update the cost of components across the thousands of dataframes according to information in a
# dataframe called Updated_price_list.
# I have given an example of Updated_price_list that contains the costs of only two components - 14 and 23.
# Dataframe Component9 contains component14. The cost of component14 needs to be updated from the price-list.
# For example, it needs to change from 55.3854 to 12.5396.
# Dataframe Component9 does not contain component 23, nor does Component2 contain either component 14 or 23,
# so no change is needed.
# I am working on a solution that uses lapply and a function.
# Placing the data frames into a list.
List <- list(Component2, Component9)
# Making a function to update the Total_Cost column.
Search_replace <- function(x){
(x$Component_Product_Code == Updated_price_list$Assembled_Product_Code)
x$Component_Product_Code <- Updated_price_list$Assembled_Product_Code
}
# Using lapply to update each data frame.
Updated_dataframes <- (lapply(List, Search_replace))
# However, this leads to errors.
# Your help in advance would be very much appreciated.