0

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?

JBL
  • 37
  • 2

1 Answers1

2

This would typically be done with a join. left_join from dplyr will connect each of the rows in your first table with the each of the matching rows in the second table.

https://dplyr.tidyverse.org/reference/join.html

library(dplyr)
Df2 %>% left_join(Df1)

Joining, by = c("A", "B")
    A B C
1 112 2 4
2 111 2 1
3 113 2 6
4 111 5 2
5 111 7 3
6 112 8 5

merge from base R will give a similar result, but doesn't keep the original row order without some extra wrangling.

Merge two data frames while keeping the original row order

merge(Df2, Df1)
    A B C
1 111 2 1
2 111 5 2
3 111 7 3
4 112 2 4
5 112 8 5
6 113 2 6
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Df2 <- left_join(Df2, df1, by = c("A", "B") ) worked. Thank you! Super helpful. – JBL Apr 25 '19 at 17:26