我正在寻找一种方法来代替 nesting_
在我的代码中,我看到该函数被标记为 Deprecated lifecycle
.
下面是我使用的示例代码 nesting_
在 complete
传递一个列名向量,并生成列与列之间所有可能的组合。a
和对 b
& c
.
library(tidyr)
library(dplyr)
data <- tibble(a = c(1, 1, 2, 2, 3),
b = c("P-1", "P-2", "P-3", "P-2", "P-4"),
c = c("O-1", "O-4", "O-2", "O-3", "O-3"))
#
nesting_variables <- c("b", "c", "c")
data %>%
complete(a, nesting_(unique(nesting_variables)))
输出
# A tibble: 15 x 3
a b c
<dbl> <chr> <chr>
1 1 P-1 O-1
2 1 P-2 O-3
3 1 P-2 O-4
4 1 P-3 O-2
5 1 P-4 O-3
6 2 P-1 O-1
7 2 P-2 O-3
8 2 P-2 O-4
9 2 P-3 O-2
10 2 P-4 O-3
11 3 P-1 O-1
12 3 P-2 O-3
13 3 P-2 O-4
14 3 P-3 O-2
15 3 P-4 O-3
你可以使用 nesting
而不是 syms
和 !!!
:
library(tidyr)
library(rlang)
data %>% complete(a, nesting(!!!syms(unique(nesting_variables))))
我们可以确认,这两种解决方案给出了相同的输出。
identical(data %>% complete(a, nesting(!!!syms(unique(nesting_variables)))),
data %>% complete(a, nesting_(unique(nesting_variables))))
#[1] TRUE