I'd like to create an efficient ifelse statement such that if columns from df2 match columns from df1, then that row in df2 is coded a specific way. My code works but is very inefficient.
Example data:
Df1
A B C
111 2 1
111 5 2
111 7 3
112 2 4
112 8 5
113 2 6
Df2
A B
112 2
111 2
113 2
111 5
111 7
112 8
Desired Outcome:
Df2
A B C
112 2 4
111 2 1
113 2 6
111 5 2
111 7 3
112 8 5
What I've done is this:
Df2$C<- ifelse(Df2$A == 111 & Df2$B == 2, 1, 0)
Df2$C<- ifelse(Df2$A == 111 & Df2$B == 5, 2, 0)
Df2$C<- ifelse(Df2$A == 111 & Df2$B == 7, 3, 0)
...
This works, but is there a way such that df2 could reference the columns in df1 and create column df2$C, so that each combination doesn't have to be manually typed out?