tidyverse / purrr

A functional programming toolkit for R

Home Page:https://purrr.tidyverse.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FR] `list_cbind()` matrices

mikmart opened this issue · comments

It would be nice if list_cbind() worked with matrices.

A <- matrix(1:4, 2, 2)
B <- matrix(5:8, 2, 2)
do.call(cbind, list(A, B))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    3    5    7
#> [2,]    2    4    6    8
purrr::list_cbind(list(A, B))
#> Error in `purrr::list_cbind()`:
#> ! Each element of `x` must be either a data frame or `NULL`.
#> ℹ Elements 1 and 2 are not.
#> Backtrace:
#>     ▆
#>  1. └─purrr::list_cbind(x = list(A, B))
#>  2.   └─purrr:::check_list_of_data_frames(x)
#>  3.     └─cli::cli_abort(...)
#>  4.       └─rlang::abort(...)

And of course also list_rbind() (forgot to mention it for the search keyword).

We don't have support for this in vctrs, so unfortunately it's a long way from coming to purrr:

A <- matrix(1:4, 2, 2)
B <- matrix(5:8, 2, 2)
str(vctrs::vec_cbind(A, B))
#> New names:
#> • `` -> `...1`
#> • `` -> `...2`
#> • `` -> `...3`
#> • `` -> `...4`
#> 'data.frame':    2 obs. of  4 variables:
#>  $ ...1: int  1 2
#>  $ ...2: int  3 4
#>  $ ...3: int  5 6
#>  $ ...4: int  7 8

Created on 2023-07-26 with reprex v2.0.2

Thanks. I presume there are no plans to add it to vctrs, as the current behaviour always results in a data frame?

Yeah, and I like the idea of keeping list_rbind() and list_cbind() closely focussed on data frames as the semantics will get complicated if you have a mix of data frames and matrices.