我正在努力学习如何使用 hadleyverse 进行编程。我已经阅读了 NSE 和lazyeval 小插曲,但我仍然迷路了......
我正在尝试将 tidyr::complete 帮助页面上给出的示例翻译为 SE 案例。
df <- data_frame(
group = c(1:2, 1),
item_id = c(1:2, 2),
item_name = c("a", "b", "b"),
value1 = 1:3,
value2 = 4:6
)
df %>% complete(group, nesting(item_id, item_name))
我的最终目标是能够用我指定的变量做同样的事情:
v1 <- 'group'
v2 <- 'item_id, item_name'
但在我尝试之前,我需要能够直接使用列名来解决它。首先,即使我知道这不是我想要的,这至少不会引发错误:
df %>% complete_(list(~group, ~item_id, ~item_name))
我想不通的是如何包含
我尝试过的“嵌套_”事情:
df %>% complete_(~group, nesting_(~item_id, ~item_name))
# Error in nesting_(~item_id, ~item_name) : unused argument (~item_name)
df %>% complete_(~group, nesting_(list(~item_id, ~item_name)))
# Error: Each variable must be named.
# Problem variables: 1, 2
df %>% complete_(~group, nesting_(alist(~item_id, ~item_name)))
# Error: Each variable must be named.
# Problem variables: 1, 2
df %>% complete_(~group, nesting_(list('item_id' = item_id, 'item_name' = item_name)))
# Error in stopifnot(is.list(x)) : object 'item_id' not found
df %>% complete_(~group, nesting_(list('item_id' = df$item_id, 'item_name' = df$item_name)))
# No syntax error, but doesn't expand...
df %>% complete_(~group, nesting_(named_dots(item_id, item_name)))
# Error: Each variable must be a 1d atomic vector or list.
# Problem variables: 'item_id', 'item_name'
df %>% complete_(~group, nesting_(list('item_id' = item_id, 'item_name' = item_name)))
# Error in stopifnot(is.list(x)) : object 'item_id' not found
df %>% complete_(~group, nesting_(list(as.name(item_id), as.name(item_name))))
# Error in as.name(item_id) : object 'item_id' not found
df %>% complete_(~group, nesting_(as.name(item_id), as.name(item_name)))
# Error in nesting_(as.name(item_id), as.name(item_name)) :
# unused argument (as.name(item_name))
谢谢你的帮助!!