This kind of helped: How to do vlookup in R
Problem: I have a list of machine numbers in the database and that need to have a machine rate associated with them (e.g. $20.00). In a CSV (machine_rates.csv) file, I have a list of those machine numbers with the associated machine rate (columns A & B, respectively).
I've tried using MERGE
for this but for some reason it creates a lot of NA's throughout the dataframe even though I have the all.x = TRUE
. It almost seems like if a machine # doesn't show up for that row, it turns the whole row into NA's. SO this leads me to believe I am not understanding the MERGE
function correctly (read through many posts trying to find the equivalent of a vlookup in R).
So here below, I tried to create a new dataframe by the merge but when merging, how do you tell it to create a new column to place those merged
machine rates?
dBase = dbReadTable(conn, "Mfng_Data")
mBase = read.csv("Machine_Rates.csv")
dBase2 = merge(dBase, mBase, by.x = "machine_number", by.y = "machine_number",
all.x = TRUE)
Edit:
Is there a way to get around listing all of the items out? dBase contains about a million records (around 1m rows x 70c matrix). So if there are 150 different machine rates, would I have to list all of those out or is it possible to "index" those values in the CSV by matching the machine number in mBase to the machine number in dBase ?