VLOOKUP in R Programming Language - Problem Statement: I need to perform a VLOOKUP in R, for monthly reports. I currently am doing it in Excel, however, I would like to do it in R.
Background:
1.) df1 = 50,000 Rows / 115 Columns – The column I want to match with: "account_number"
A.) df1$account_number
2.) df2 = 11,000 lines / 2 columns – The column I want to match against: "account_number_1"
B.) df2$account_number_1
3.) df1$flag – Where I want a “Y” or “N” for the match of df1$account_number == df2$account_number_1
C.) df1$flag
Research Attempts:
• I've been searching, attempting, and trying to code what I thought would be easy, however, I do not know why it is so difficult to do.
• My searches, and trial-and-error have resulted to Merge, Match, Duplicate and other methods that I cannot remember, and none of them give me exactly what I need. Any assistance on what I am doing wrong, and/or how to complete a VLOOKUP for the desired result is much appreciated! Thanks!
I thought I would get it through this StackOverflow link, however, I still cannot get the desired results:
How to do vlookup and fill down (like in Excel) in R?
Here are my searches and script attempts:
Merge: # COMBINE 2 Dataframes:
Combine two data frames by rows (rbind) when they have different sets of columns by-rows-rbind-when-they-have-different-sets-of-columns
(FYI: cbind, left_join, inner_join - all give me errors, however, "smartbind" WORKS!!!)
df_merge <- smartbind(df1, df2)
My attempt to TRUE/FALSE – cbind and rbind # https://www.youtube.com/watch?v=NFaK1Qn4u3A - Logic Statements (TRUE/FALSE) and cbind and rbind Command in R (R Tutorial 1.9)
df_merge$flag <- df_merge$account_number == df_merge$account_number_1
https://www.youtube.com/watch?v=LKoknpFOEUw - Search "in R how to match values"
duplicated(df_merge) which(duplicated(df_merge))
https://www.youtube.com/watch?v=eVEx_pBEkRI
df_merge$flag <- any(df_merge$account_number == df_merge$account_number_1)
any(df_merge$account_number == df_merge$account_number_1)
if (which(duplicated(df_merge$account_number == df_merge$account_number_1))) { df_merge$flag <- "Y" } else if (which(duplicated(df_merge$account_number != df_merge$account_number_1)) { status <- "N" }
I attempted these links, and got lost:
How to do vlookup and fill down (like in Excel) in R?
http://stat.ethz.ch/R-manual/R-devel/library/base/html/match.html
match(x, table, nomatch = NA_integer_, incomparables = NULL)
x %in% table
df_match <- match(df_merge$account_number , df_merge$ account_number_1, nomatch=NA_integer_, incomparables=NULL)