我正在学习如何使用purrr,并认为它对跟踪某些计算会很有用。
但是,我不确定为什么不能使用涉及以下组件的purrr :: pmap进行特定操作:
列出每个元素的长度为n 长度为1的向量 长度为1的向量 长度为n的向量 1.,2和3.都在同一数据帧中(名为“ operations_df”)。 4.在数据帧之外,但是是每个列表元素长度相同(长度都相同)的向量。因此,函数调用基本上涉及将向量1.中的每个元素乘以4.中的每个元素,然后将所得的1个元素向量与2和3相加/相减。
如果我用map2函数分解,这很好用。但是我想知道如何使它与pmap一起使用?
library(purrr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# generate data
data <- rbeta(n = 10, shape1 = 80, shape2 = 80)
prob_k1 <- rbeta(n = 10, shape1 = 80, shape2 = 10)
prob_k2 <- 1-prob_k1
# perform operations on prob_k and data in a data.frame
operations_df <- tibble(components = c('1', '2'),
probability = list(prob_k1, prob_k2)) %>%
# sum over list column
mutate(n = map_dbl(probability, sum)) %>%
# mean for each row, using list column and a single 1-element vector
mutate(mu = map2_dbl(probability, n, ~ (1/.y) * sum(data * .x)))
operations_df
#> # A tibble: 2 x 4
#> components probability n mu
#> <chr> <list> <dbl> <dbl>
#> 1 1 <dbl [10]> 8.93 0.504
#> 2 2 <dbl [10]> 1.07 0.506
# this doesn't work
# variance for each row, using list column, and two 1-element vectors
operations_df %>%
mutate(var = pmap_dbl(probability, n, mu, ~ (1/(..2-1)) * sum(..1 * data^2) - ..3^2))
#> Result 1 must be a single double, not NULL of length 0
# this does work
(1/(operations_df$n[1]-1)) * sum(operations_df$probability[[1]] * data^2) - operations_df$mu[1]^2
#> [1] 0.0342961
(1/(operations_df$n[2]-1)) * sum(operations_df$probability[[2]] * data^2) - operations_df$mu[2]^2
#> [1] 3.800814
# breaking it up into two map2 calls works:
operations_df %>%
mutate(var = map2_dbl(n, probability, ~ (1/(.x-1)) * sum(.y * data^2))) %>%
mutate(var = map2_dbl(var, mu, ~ .x - .y^2))
#> # A tibble: 2 x 5
#> components probability n mu var
#> <chr> <list> <dbl> <dbl> <dbl>
#> 1 1 <dbl [10]> 8.93 0.504 0.0343
#> 2 2 <dbl [10]> 1.07 0.506 3.80
答案 0 :(得分:1)
pmap()
只接受一个参数列表,而不像map()
或map2()
那样一次接受一个参数,因此在运行mutate之前,您需要在列表中添加参数。
library(purrr)
library(dplyr)
set.seed(10)
# generate data
data <- rbeta(n = 10, shape1 = 80, shape2 = 80)
prob_k1 <- rbeta(n = 10, shape1 = 80, shape2 = 10)
prob_k2 <- 1-prob_k1
# perform operations on prob_k and data in a data.frame
operations_df <- tibble(components = c('1', '2'),
probability = list(prob_k1, prob_k2)) %>%
mutate(n = map_dbl(probability, sum)) %>%
mutate(mu = map2_dbl(probability, n, ~ (1/.y) * sum(data * .x)))
# pmap only takes a list and not parameters one at a time like map & map2
# see ?pmap for more deetz
operations_df %>%
mutate(var = pmap_dbl(list(probability, n, mu), ~(1/(..2-1)) * sum(..1 * data^2) - ..3^2))
#> # A tibble: 2 x 5
#> components probability n mu var
#> <chr> <list> <dbl> <dbl> <dbl>
#> 1 1 <dbl [10]> 8.77 0.476 0.0303
#> 2 2 <dbl [10]> 1.23 0.479 1.01
# This produces the same output without the complication of thinking about mutate().
list(operations_df$probability,
operations_df$n,
operations_df$mu) %>%
pmap_dbl(~(1/(..2-1)) * sum(..1 * data^2) - ..3^2)
#> [1] 0.0303307 1.0087492
由reprex package(v0.2.1)于2019-06-06创建