dplyr - 在函数 left_join(y, by = c(x="value")) 中评估字符串
dplyr - evaluating a string in a function left_join(y, by = c(x="value"))
我不理解的是,使用下面的例子,我认为:
df2 <- df1 %>% left_join(code_label, by = c("team"="code"))
并且,在函数中,
df2 <- df1 %>% left_join(code_label, by = c(x='code'))
会被相同地评估,但它们不是,有没有办法包装 'x' 所以它们是?
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"))
给出一个 result.But 我想将 df1 的列名“team”作为未命名对象(我认为是正确的描述?)传递给如下函数:
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'))
}
但它产生错误:
Error: Join columns must be present in data.
x Problem with `x`
我查看了有关 enquo 等的文档,这对我来说是新的。但是函数中的 print(x) 已经 returns [1] “team”。 但和我想的不太一样。
我们可以使用具有 setNames
的命名向量
f_show_labels_with_codes <- function(x) {
print(x)
df1 %>%
left_join(code_label, by = setNames('code', x))
}
-测试
> 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
我不理解的是,使用下面的例子,我认为:
df2 <- df1 %>% left_join(code_label, by = c("team"="code"))
并且,在函数中,
df2 <- df1 %>% left_join(code_label, by = c(x='code'))
会被相同地评估,但它们不是,有没有办法包装 'x' 所以它们是?
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"))
给出一个 result.But 我想将 df1 的列名“team”作为未命名对象(我认为是正确的描述?)传递给如下函数:
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'))
}
但它产生错误:
Error: Join columns must be present in data.
x Problem with `x`
我查看了有关 enquo 等的文档,这对我来说是新的。但是函数中的 print(x) 已经 returns [1] “team”。
我们可以使用具有 setNames
f_show_labels_with_codes <- function(x) {
print(x)
df1 %>%
left_join(code_label, by = setNames('code', x))
}
-测试
> 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