Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用函数参数在函数内部执行 left_join 来告诉代码应该是“by.x”和“by.y”。在下面的示例中,我想使用函数“aa”的“a2”和“a3”参数 - 代替“by”参数中的“x1”和“a”。我很困惑如何在这里使用 rlang 包的好处
aa <- function(a1,a2,a3){ a1 %>% left_join(a1, by=c("x1"="a")) } xx<-data.frame(a=c(1:2), x1=c(2:3)) aa(xx,"x1","a")
如果我正确理解了您的问题并且您的目标是能够将您要加入的列的名称传递给函数,则以下内容应该可以工作:
aa <- function(a1,a2,a3){ a1 %>% left_join(a1, by=setNames(a3, a2)) } xx<-data.frame(a=c(1:2), x1=c(2:3)) aa(xx,"x1","a")
在这种情况下,通过 using 指定列c(a2=a3)不起作用,我将其替换为setNames(a3, a2).
c(a2=a3)
setNames(a3, a2)