dplyr: NSE in joins (by)

牧云@^-^@ 提交于 2020-05-12 04:28:26

问题


I had a hard time to figure out how I could join two tables using dplyr::left_join with NSE. The problem was that I could not supply the right value to 'by'. I think I have found a solution for now, but it feels like I am doing it in an extra complicated way. So, if you know an easier/more elegant solution, please let me know :)

That's what I am doing:

# some data
df <- dplyr::tibble(x=1:10,
                    y=LETTERS[1:10],
                    z=LETTERS[11:20])

# some function
test_fun <- function(df,id){
  id <- rlang::enquo(id)
  join_var <- names(rlang::quos_auto_name(id))[2] # is there an easier way?
  # not important: doing some stuff
  df1 <- df %>%
    dplyr::select(!!id,y)
  df2 <- df %>%
    dplyr::select(!!id,z)
  # join using prepared join_var
  df1 %>%
    dplyr::left_join(df2,setNames(join_var,join_var))
}

test_fun(df,id=x)

As I was asked for additional context and some more explaination to better understand my problem, I will try to give some more information here:

NSE is an abbreviation for non-standard evaluation. My goal was to build a function that joins to datasets based on a variable the user could define. The main problem was that the by-argument in dplyr::left_join did not accept the quoted variable (NSE argument) and so I had to find a workaround. The way I found (join_var <- names(rlang::quos_auto_name(id))[2]) seemd a little complicated and I was looking for an easier solution. That was - by the way - provided by Lionel Henry ... thanks :)


回答1:


You're looking for join_var <- rlang::as_name(id).



来源:https://stackoverflow.com/questions/58518021/dplyr-nse-in-joins-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!