是否有一种基于名称向量添加多个列的稳定(未弃用)dplyr方法?
假设我有一个简单的data.frame:是否有一种基于名称向量添加多个列的稳定(未弃用)dplyr方法?,r,dplyr,assignment-operator,R,Dplyr,Assignment Operator,假设我有一个简单的data.frame: > d <- data.frame(A=1, B=2) 但对于一个以上的名称,它不起作用: > d %>% mutate({{multip_name}} := NA) Error: The LHS of `:=` must be a string or a symbol Run `rlang::last_error()` to see where the error occurred. > d %>% mutate
> d <- data.frame(A=1, B=2)
但对于一个以上的名称,它不起作用:
> d %>% mutate({{multip_name}} := NA)
Error: The LHS of `:=` must be a string or a symbol
Run `rlang::last_error()` to see where the error occurred.
> d %>% mutate(!!!syms(multip_name) := NA)
Error: The LHS of `:=` can't be spliced with `!!!`
Run `rlang::last_error()` to see where the error occurred.
它不必是“变异”,请提出以下建议:
- 来自tidyverse
- 不依赖任何“被取代”、“不推荐”的东西
d %>%
add_column(!!!setNames(rep(NA, length(multip_name)), multip_name))
A B x y z
1 1 2 NA NA NA
这回答了你的问题吗?
> d %>% mutate(!!sym(single_name) := NA)
A B x
1 1 2 NA
> d %>% mutate({{single_name}} := NA)
A B x
1 1 2 NA
> d %>% mutate({{multip_name}} := NA)
Error: The LHS of `:=` must be a string or a symbol
Run `rlang::last_error()` to see where the error occurred.
> d %>% mutate(!!!syms(multip_name) := NA)
Error: The LHS of `:=` can't be spliced with `!!!`
Run `rlang::last_error()` to see where the error occurred.
d %>%
add_column(!!!setNames(rep(NA, length(multip_name)), multip_name))
A B x y z
1 1 2 NA NA NA