Repeated values cause unexpected data lengthening when merging.
df1 <- data.frame(col1 = LETTERS[1:4],
col2 = 1:4)
df1 %>% kable() %>% kable_styling()
col1 | col2 |
---|---|
A | 1 |
B | 2 |
C | 3 |
D | 4 |
df2 <- data.frame(col1 = rep(LETTERS[1:2], 2),
col3 = 4:1)
df2 %>% kable() %>% kable_styling()
col1 | col3 |
---|---|
A | 4 |
B | 3 |
A | 2 |
B | 1 |
I might expect four rows after merging, but instead I get six.
df <- left_join(df1, df2)
df %>% kable() %>% kable_styling()
col1 | col2 | col3 |
---|---|---|
A | 1 | 4 |
A | 1 | 2 |
B | 2 | 3 |
B | 2 | 1 |
C | 3 | NA |
D | 4 | NA |
Bo\(^2\)m =)