我建立了一个似乎可以正常工作的函数,但我不明白为什么。
我最初的问题是获取一个包含总体计数的data.frame并将其扩展以重新创建原始总体。如果您事先知道列名,这很容易。
library(tidyverse)
set.seed(121)
test_counts <- tibble(Population = letters[1:4], Length = c(1,1,2,1),
Number = sample(1:100, 4))
expand_counts_v0 <- function(Length, Population, Number) {
tibble(Population = Population,
Length = rep(Length, times = Number))
}
test_counts %>% pmap_dfr(expand_counts_v0) %>% # apply it
group_by(Population, Length) %>% # test it
summarise(Number = n()) %>%
ungroup %>%
{ all.equal(., test_counts)}
# [1] TRUE
但是,我想将其概括为一个不需要在data.frame的列名上知道的函数,并且我对NSE感兴趣,所以我写道:
test_counts1 <- tibble(Population = letters[1:4],
Length = c(1,1,2,1),
Number = sample(1:100, 4),
Height = c(100, 50, 45, 90),
Width = c(700, 50, 60, 90)
)
expand_counts_v1 <- function(df, count = NULL) {
countq <- enexpr(count)
names <- df %>% select(-!!countq) %>% names
namesq <- names %>% map(as.name)
cols <- map(namesq, ~ expr(rep(!!., times = !!countq))
) %>% set_names(namesq)
make_tbl <- function(...) {
expr(tibble(!!!cols)) %>% eval(envir = df)
}
df %>% pmap_dfr(make_tbl)
}
但是,当我测试此功能时,它似乎将行重复了4次:
test_counts %>% expand_counts_v1(count = Number) %>%
group_by(Population, Length) %>%
summarise(Number = n()) %>%
ungroup %>%
{ sum(.$Number)/sum(test_counts$Number)}
# [1] 4
这使我猜出了一个解决方案,即
expand_counts_v2 <- function(df, count = NULL) {
countq <- enexpr(count)
names <- df %>% select(-!!countq) %>% names
namesq <- names %>% map(as.name)
cols <- map(namesq, ~ expr(rep(!!., times = !!countq))
) %>% set_names(namesq)
make_tbl <- function(...) {
expr(tibble(!!!cols)) %>% eval(envir = df)
}
df %>% make_tbl
}
这似乎可行:
test_counts %>% expand_counts_v2(count = Number) %>%
group_by(Population, Length) %>%
summarise(Number = n()) %>%
ungroup %>%
{ all.equal(., test_counts)}
# [1] TRUE
test_counts1 %>% expand_counts_v2(count = Number) %>%
group_by(Population, Length, Height, Width) %>%
summarise(Number = n()) %>%
ungroup %>%
{ all.equal(., test_counts1)}
# [1] TRUE
但是我不明白为什么。即使我不再使用pmap,它如何评估每一行?该功能需要应用到每一行才能正常工作,因此必须采用某种方式,但我看不出它是如何实现的。
编辑
Artem对发生的事情进行了正确的解释后,我意识到我可以做到这一点
expand_counts_v2 <- function(df, count = NULL) {
countq <- enexpr(count)
names <- df %>% select(-!!countq) %>% names
namesq <- names %>% map(as.name)
cols <- map(namesq, ~ expr(rep(!!., times = !!countq))
) %>% set_names(namesq)
expr(tibble(!!!cols)) %>% eval_tidy(data = df)
}
摆脱了不必要的mk_tbl函数。但是,正如Artem所说,这只是真正有效,因为rep是矢量化的。因此,它正在工作,但无法通过重写_v0函数并对其进行映射来完成,这是我试图复制的过程。最终,我发现了rlang :: new_function并写道:
expand_counts_v3 <- function(df, count = NULL) {
countq <- enexpr(count)
names <- df %>% select(-!!countq) %>% names
namesq <- names %>% map(as.name)
cols <- map(namesq, ~ expr(rep(!!., times = !!countq))
) %>% set_names(namesq)
all_names <- df %>% names %>% map(as.name)
args <- rep(0, times = length(all_names)) %>% as.list %>% set_names(all_names)
correct_function <- new_function(args, # this makes the function as in _v0
expr(tibble(!!!cols)) )
pmap_dfr(df, correct_function) # applies it as in _v0
}
更长,也许更丑,但是按照我最初想要的方式工作。
答案 0 :(得分:0)
问题出在eval( envir = df )
中,该问题将整个数据帧暴露给make_tbl()
。请注意,您永远不会在...
中使用make_tbl()
参数。相反,该函数有效地计算了
with( df, tibble(Population = rep(Population, times = Number),
Length = rep(Length, times=Number)) )
无论您提供什么参数。当您通过pmap_dfr()
调用该函数时,它实际上会计算上述四次(每行一次)并按行连接结果,从而导致您观察到的条目重复。当您删除pmap_dfr()
时,该函数将被调用一次,但是由于rep
本身是矢量化的(请尝试执行rep( test_counts$Population, test_counts$Number )
以了解我的意思),make_tbl()
会在一口气。