Home > Enterprise >  How to join data.frames with paste() in the 'by' input?
How to join data.frames with paste() in the 'by' input?

Time:03-14

How do I make this work?

test <- data.frame(a = 1:5,
               b = 6:10)

test2 <- data.frame(c = 11:15,
                bb = 6:10)

test %>% left_join(test2, by = c(paste0('b', paste(rep('b', 1), collapse = '')) = 'b'))

Currently I get the error: Error: unexpected '=' in "test %>% left_join(test2, by = c(paste0('b', paste(rep('b', j), collapse = '')) ="

In the rep() function is currently 1 but the idea is to change it to an index later to use the same code for different inputs.

CodePudding user response:

If you are using the a = b syntax for the by statement you need to have it within a c(), e.g. by = c(a = b), rather than by = a = b which will generate a syntax error.

In your case I'd also assign your complicated paste0 to a variable so the code is more readable.

This should work:

test <- data.frame(a = 1:5,
               b = 6:10)

test2 <- data.frame(c = 11:15,
                bb = 6:10)

col_to_join = c(paste0('b', paste(rep('b', 1), collapse = '')))
test %>% left_join(test2, by = c("b" = col_to_join))

CodePudding user response:

You have a few issues, but this code does what you want I think.



test %>% left_join(test2, 
   by = c("b" = paste0('b', paste(rep('b', 1), collapse = ''))))

First, put the column names in the correct order. Second, those names need to be inside a vector. Third, the c() around the paste is unneeded.

  • Related