Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一种基于名称向量添加多个列的稳定(未弃用)dplyr方法?_R_Dplyr_Assignment Operator - Fatal编程技术网

是否有一种基于名称向量添加多个列的稳定(未弃用)dplyr方法?

是否有一种基于名称向量添加多个列的稳定(未弃用)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

假设我有一个简单的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(!!!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