joining dataframes - using variables in left_join "by"

Hi this is part of trying to introduce myself to functions but I think I should try to get the basics working first. I don't think I'm using the right terminology to search for the answer so hope someone can help please. I don't know how to use a variable in "by" when joining two dataframes, eg,
col.A = "fruit"
col.B = "colour"

table1 <- table2 %>% left_join(table3, by=c(col.A=col.B))

error message with this: "Join columns in x must be present in the data."

Could you point me in the right direction please? Thanks

The book R for Data Science (2nd edition) which you can read for free is highly recommended to read through when learning the basics. There is a good chapter on joins that I think will help you with your current problem!


thanks, that is indeed a great resource, thank you, although in this case the solution to using objects in join_by wasn't in that section - it seems to be
table1 <- table2 %>% left_join(table3, join_by = setNames(col.A,col.B))
Think I need to read a lot more on creating functions!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.