I am trying to recode data between data frames using a for loop and I keep getting errors.
For background, df1[,1] is called 'Code' and has 111 different observations and df1[,2] is called 'KDE Code'. df2[,1] is also called 'Code' and has 14000 observations (some have the same values df1 and some do not). I would like to recode the values in df2[,1] based on values in df1.
For example, if df2[,1] == df1[,2] then I want to recode it to match what is in df1[,1] but keep the others the same (so that I can merge them later.
A for real example is that a school is identified as 012301 in df1, but df2 tells me that school 012301 should be 14916. I have tried a for loop to change them, but am having no luck.
Here is my code, and I would appreciate any help!
for (i in 1:258) {
ifelse(df2[,1] == df1[i,3], df2[,1] <- df1[i,2], df2[,1])
}
This is what I have
<pre><code>`df1
[,1] [,2]
[1,] 1 101
[2,] 2 202
[3,] 3 303`
<pre><code>`df2
[,1]
[1,] 101
[2,] 202
[3,] 303
[4,] 404`
This is what I need
<pre><code>`df2
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 404`