簡體   English   中英

列表列上的purrr pmap和幾個向量

[英]purrr pmap on a list-column, and several vectors

我正在學習如何使用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

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軟件包 (v0.2.1)創建於2019-06-06

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM