I have two data frames and want to join them on a combination of two columns in each.
For example, let's say I have these two tibbles:
df1 <- tibble(v1 = c('1', '2', '3', '4', '5', '6'), c('20', '22', '24', '26', '28', '30'))
df2 <- tibble(v1 = c('01', '02', '03', '04', '05', '06'), c('20', '22', '24', '26', '28', '30'))
And let's say v1
cycles through 1-6 for various values of v2
. My thinking is I should combine v1
and v2
to a master id var and then I can join the two dataframes. But the issue is that in df1
the numbers below 10 have no 0
in front of them but in df2
there are 0
s. So as a solution I want to add a zero to the single-digit numbers in df1
.
This is what I have so far, but it's not quite working:
df1 <- df1 %>%
mutate(idvar = str_c(v1, '.', v2)) %>%
if str_length(idvar) == 4{
mutate(idvar = str_sub(.$idvar, 1, regexpr(" O", .$idvar)-1))
}
Any idea where i'm going wrong?
The aim would be to then run:
df1 %>% left_join(df2, by = "idvar")