0

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 0s. 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")
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
C.Robin
  • 1,085
  • 1
  • 10
  • 23

2 Answers2

2

We can use sprintf

library(dplyr)
df1 <- df1 %>%
    mutate(v1 = sprintf('%02d', as.numeric(v1)))

Or use str_pad

library(stringr)
df1 %>% 
    mutate(v1 = str_pad(v1, width = 2, pad = '0'))

-output

# A tibble: 6 x 2
  v1    v2   
  <chr> <chr>
1 01    20   
2 02    22   
3 03    24   
4 04    26   
5 05    28   
6 06    30   
akrun
  • 874,273
  • 37
  • 540
  • 662
2

You can use str_pad:

library(stringr) 
str_pad(df1$v1, width = 2, side = "left", "0")
[1] "01" "02" "03" "04" "05" "06"
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34