Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

left_join(): by = c("key1" = key2) first key variable quotes don't matter, second key must be quoted #3361

Closed
erikerhardt opened this issue Feb 15, 2018 · 4 comments

Comments

@erikerhardt
Copy link

Inconsistency with left_join()'s by= argument, variable quotes are inconsistently parsed when matching different variables between first and second tibble. Quotes on first key don't matter, but the second key must be quoted.

##  version  R version 3.4.3 (2017-11-30)
##  dplyr        * 0.7.4    2017-09-28 CRAN (R 3.4.2)
library(tidyverse)

df1 <- tibble(key1 = c(1, 2, 3), val1 = c(letters[key1]))
df2 <- tibble(key2 = c(1, 2, 4), val2 = c(letters[key2]))

# quote both
df1 %>% left_join(df2, by = c("key1" = "key2"))

# quote second
df1 %>% left_join(df2, by = c(key1 = "key2"))

# quote first
# df1 %>% left_join(df2, by = c("key1" = key2))
## Error in common_by(by, x, y) : object 'key2' not found

# quote none
# df1 %>% left_join(df2, by = c(key1 = key2))
## Error in common_by(by, x, y) : object 'key2' not found
@hongooi73
Copy link

hongooi73 commented Feb 15, 2018

This is just how R's parser works. c(a=b) is a call to the c function, with an argument named a whose value is the contents of the object b. If b doesn't exist, it will throw an error. c("a"="b") still has an argument named a (because strings are magically promoted to names when on the LHS of an equal in a function call), but now its value is the string "b".

@batpigandme
Copy link
Contributor

I believe @Hong-Revo has it right. If you look at Controlling how tables are matched from the dplyr docs or Defining the key columns in R4DS, you'll see that the by argument (when not NULL/a natural join) is a character vector or a named character vector.

@krlmlr
Copy link
Member

krlmlr commented Feb 28, 2018

Thanks. We'll be working towards a better syntax in #2240.

@lock
Copy link

lock bot commented Aug 27, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Aug 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants