Home > database >  dplyr - evaluating a string in a function left_join(y, by = c(x="value"))
dplyr - evaluating a string in a function left_join(y, by = c(x="value"))

Time:12-17

My lack of understanding is, using the example that follows, I thought that:

df2 <- df1 %>% left_join(code_label, by = c("team"="code"))

and, in a function,

df2 <- df1 %>% left_join(code_label, by = c(x='code'))

would be evaluated identically but they aren't, is there a way of wrapping 'x' so they are?

df1_id <- c(1, 2, 3, 4, 5, 6)
team <- c(1, 2, 1, 6, 4, 1)
year <- c(2014, 2014, 2009, 2020, 2015, 2017)

df1 <- data.frame(df1_id, team, year)

code_id <- c(1,2,3,4,5,6)
code <- c(1,2,3,4,5,6)
label <- c("team_A", "team_B","team_C","team_D","team_E","team_F")

code_label <- data.frame(code_id, code,label)

df2 <- df1 %>% left_join(code_label, by = c("team"="code"))

gives a result.But I want to pass the column name "team" of df1 as an un-named object (I think is the right description?) to a function as follows:

f_show_labels_with_codes(x = "team")  



f_show_labels_with_codes <- function(x) 
  
{

print(x)

    df2 <- df1 %>% left_join(code_label, by = c(x='code')) 

}

but it generates an error:

Error: Join columns must be present in data.
x Problem with `x`

I had a look at the documentation around enquo etc, it's new to me. But print(x) within the function it already returns [1] "team". This question looks similar but is not quite the same I think.

CodePudding user response:

We may use a named vector with setNames

f_show_labels_with_codes <- function(x) {

   print(x)

    df1 %>%
     left_join(code_label, by = setNames('code', x)) 
    

}

-testing

> f_show_labels_with_codes(x = "team")  
[1] "team"
  df1_id team year code_id  label
1      1    1 2014       1 team_A
2      2    2 2014       2 team_B
3      3    1 2009       1 team_A
4      4    6 2020       6 team_F
5      5    4 2015       4 team_D
6      6    1 2017       1 team_A
  • Related