dplyr:NSE加入中(作者) [英] dplyr: NSE in joins (by)

查看:68
本文介绍了dplyr:NSE加入中(作者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何使用带有NSE的dplyr :: left_join连接两个表.问题是我无法为"by"提供正确的值.我想我现在已经找到了解决方案,但是感觉就像我在以一种更加复杂的方式来做.因此,如果您知道更简单/更优雅的解决方案,请告诉我:)

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 :)

这就是我正在做的:

# 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是非标准评估的缩写.我的目标是建立一个基于用户可以定义的变量连接到数据集的函数.主要问题是dplyr :: left_join中的按参数不接受带引号的变量(NSE参数),因此我必须找到一种解决方法.我发现的方法(join_var<-名称(rlang :: quos_auto_name(id))[2])似乎有点复杂,我正在寻找更简单的解决方案.顺便说一下,那是莱昂内尔·亨利(Lionel Henry)提供的...谢谢:)

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 :)

推荐答案

您正在寻找join_var <- rlang::as_name(id).

这篇关于dplyr:NSE加入中(作者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆