I have gone through many threads about it but they don't seem to have the solution I really need. Just wanted to comment on the differences of my question from suggested posts:
- Replace contents of factor column in R dataframe is different from what I was asking. My real dataset and lookup table were a lot bigger so replace them one by one in the code was really not an option.
- Replace values in a dataframe based on lookup table this is also different since it has all combination there without unmatched entries so it's also different.
I have two data frames:
> main <- data.frame(V1 = factor(c("A","B","C","C","D","E","A")))
> main
V1
A
B
C
C
D
E
A
> lookup <- data.frame(V1=c("A","B","C"),V2=c("aa","bb","cc"))
> lookup
V1 V2
A aa
B bb
C cc
What I need is to use lookup
to update main
but leave the unmatched ones as is. Many of the answers involved using match
but it created an NA
for unmatched levels. For example, one of the solution was:
> main$V1=lookup[match(main$V1,lookup$V1),"V2"]
> main
V1
aa
bb
cc
cc
<NA>
<NA>
aa
The desired outcome is to leave the unmatched ones unchanged:
V1
aa
bb
cc
cc
D
E
aa
That was an example and my real datasets were a lot bigger so replace them one by one was really not an option. Any help or pointer will be greatly appreciated. Thanks much!