-2

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`
John
  • 1
  • 2
  • 1
    Can you provide a reproductible example? For example using `dput` – Emmanuel-Lin Aug 29 '17 at 13:59
  • Hi John, it is easier to help you if you can provide a [reproducible example](https://stackoverflow.com/a/5963610/1870254). That being said, it seems like typing `?merge` might help you. – jan-glx Aug 29 '17 at 14:05
  • I tried merge, but since the values are not the same for everything, will it still work? – John Aug 29 '17 at 14:50

1 Answers1

0

Here is a quick and dirty solution:

df1<-data.frame(pre=c(1, 2, 3), code=c(101, 202, 303))
df2<-data.frame(code=c(101, 202, 303, 404))

library(dplyr)
#Merge columns
newdf<-left_join(df2, df1)
#find NA rows and replace by first column
newdf$pre[is.na(newdf$pre)]<-newdf$code[is.na(newdf$pre)]
#drop the extra column
newdf<-data.frame(pre=newdf[,-1])

It will generate your desired "pre" column in as a dataframe.

Dave2e
  • 22,192
  • 18
  • 42
  • 50