Search code examples
rtidyversepurrr

map_dbl returns a numeric matrix


Here is a sample code I'm stuck in.

I define a function. Input a number x, output a vector vec

fct <- function(x){
  vec = vector("numeric",3)
  vec = c(1, x, x^2)
  return(vec)
}

I have a sequence a = 1:10, then I wish to apply fct on a. Ideally, it should return a numeric matrix. I know using sapply(a,fct) can achieve the purpose. But I'm curious how about using map_*.

I tried map(a,fct). It returns a list.

I tried map_dbl(a,fct). It returned an error

Error: Result 1 must be a single double, not a double vector of length 3
Run `rlang::last_error()` to see where the error occurred.

So how this can be done with map_*?


Solution

  • map_* functions don't do this easily. I suggest using the tool that works best and into a known format/structure.

    If you're motivation for using map_dbl is type-safety (a strength of the purrr::map* functions), then you can achieve the same safety with base R:

    vapply(a, fct, numeric(3))
    #      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    # [1,]    1    1    1    1    1    1    1    1    1     1
    # [2,]    1    2    3    4    5    6    7    8    9    10
    # [3,]    1    4    9   16   25   36   49   64   81   100
    

    Granted, vapply requires you to know both the class and the length of the return value a priori, but if that's not a problem, then you're good. Additionally, it's generally pretty fast:

    a <- 1:10
    bench::mark(vapply(a, fct, numeric(3)), lapply(a, fct), sapply(a, fct), map(a, fct), check = FALSE)
    # # A tibble: 4 x 13
    #   expression                      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result memory                 time              gc                   
    #   <bch:expr>                 <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list> <list>                 <list>            <list>               
    # 1 vapply(a, fct, numeric(3))   12.3us   18.1us    44793.      288B     0    10000     0      223ms <NULL> <Rprofmem[,3] [1 x 3]> <bch:tm [10,000]> <tibble [10,000 x 3]>
    # 2 lapply(a, fct)               11.6us   16.2us    52398.        0B     5.24  9999     1      191ms <NULL> <Rprofmem[,3] [0 x 3]> <bch:tm [10,000]> <tibble [10,000 x 3]>
    # 3 sapply(a, fct)               24.8us     36us    22943.      576B     2.29  9999     1      436ms <NULL> <Rprofmem[,3] [2 x 3]> <bch:tm [10,000]> <tibble [10,000 x 3]>
    # 4 map(a, fct)                  24.2us   32.4us    24356.        0B     2.44  9999     1      411ms <NULL> <Rprofmem[,3] [0 x 3]> <bch:tm [10,000]> <tibble [10,000 x 3]>
    

    (Note that the performance differences almost go away as the length of a increases.)

    But going back to map*, you can use purrr with only a small outer function,

    invoke(cbind, map(a, fct))
    #      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    # [1,]    1    1    1    1    1    1    1    1    1     1
    # [2,]    1    2    3    4    5    6    7    8    9    10
    # [3,]    1    4    9   16   25   36   49   64   81   100
    

    And its performance, though slower, is not a lot slower, and with larger data the differences are reduced.

    a <- 1:10
    bench::mark(vapply(a, fct, numeric(3)), sapply(a, fct), do.call(cbind, lapply(a, fct)), do.call(cbind, map(a, fct)), invoke(cbind, map(a, fct)), matrix(flatten_dbl(map(a, fct)), ncol = length(a)))
    # # A tibble: 6 x 13
    #   expression                                              min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result             memory              time            gc                
    #   <bch:expr>                                         <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>             <list>              <list>          <list>            
    # 1 vapply(a, fct, numeric(3))                           12.3us     17us    51210.      288B     5.12  9999     1      195ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [1 x~ <bch:tm [10,00~ <tibble [10,000 x~
    # 2 sapply(a, fct)                                       25.2us   36.4us    22276.      576B     2.23  9999     1      449ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [2 x~ <bch:tm [10,00~ <tibble [10,000 x~
    # 3 do.call(cbind, lapply(a, fct))                       16.2us   22.7us    37430.      288B     3.74  9999     1      267ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [1 x~ <bch:tm [10,00~ <tibble [10,000 x~
    # 4 do.call(cbind, map(a, fct))                          31.8us   43.6us    19856.      288B     4.40  9026     2      455ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [1 x~ <bch:tm [9,028~ <tibble [9,028 x ~
    # 5 invoke(cbind, map(a, fct))                           35.8us   49.6us    17440.      288B     2.07  8408     1      482ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [1 x~ <bch:tm [8,409~ <tibble [8,409 x ~
    # 6 matrix(flatten_dbl(map(a, fct)), ncol = length(a))   29.6us   45.2us    19309.      864B     2.08  9292     1      481ms <dbl[,10] [3 x 10~ <Rprofmem[,3] [3 x~ <bch:tm [9,293~ <tibble [9,293 x ~
    
    a <- 1:1000
    bench::mark(vapply(a, fct, numeric(3)), sapply(a, fct), do.call(cbind, lapply(a, fct)), do.call(cbind, map(a, fct)), invoke(cbind, map(a, fct)), matrix(flatten_dbl(map(a, fct)), ncol = length(a)))
    # # A tibble: 6 x 13
    #   expression                                              min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result                 memory               time         gc              
    #   <bch:expr>                                         <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>                 <list>               <list>       <list>          
    # 1 vapply(a, fct, numeric(3))                           1.05ms   1.35ms      714.    23.5KB     4.33   330     2    461.9ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [1 x ~ <bch:tm [33~ <tibble [332 x ~
    # 2 sapply(a, fct)                                       1.13ms   1.41ms      655.    70.8KB     4.31   304     2      464ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [6 x ~ <bch:tm [30~ <tibble [306 x ~
    # 3 do.call(cbind, lapply(a, fct))                        1.4ms    1.7ms      543.    31.3KB     4.33   251     2    462.1ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [2 x ~ <bch:tm [25~ <tibble [253 x ~
    # 4 do.call(cbind, map(a, fct))                          1.66ms   1.78ms      479.    31.3KB    10.2     47     1     98.1ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [2 x ~ <bch:tm [48~ <tibble [48 x 3~
    # 5 invoke(cbind, map(a, fct))                            1.3ms   1.73ms      546.    39.2KB     4.30   254     2    464.8ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [3 x ~ <bch:tm [25~ <tibble [256 x ~
    # 6 matrix(flatten_dbl(map(a, fct)), ncol = length(a))   1.13ms   1.49ms      551.    78.3KB     2.07   266     1    482.8ms <dbl[,1000] [3 x 1,00~ <Rprofmem[,3] [4 x ~ <bch:tm [26~ <tibble [267 x ~
    

    (The output of the six commands are identical.)