Skip to content

Allow matrix and data frame columns #416

Closed

Description

@hadley

i.e. relax the length() restriction to be an NROW() restriction

Activity

  1. krlmlr commented on Jun 6, 2018

    @krlmlr
    Member

    I've seen tidyverse/dplyr#3630, and I wonder what's the rationale. One advantage I see is that we can support POSIXlt columns (and other related data-frame-like data types). For matrices and data frames, I wonder if as_tibble() could flatten these instead (with a consistent naming scheme) so that the columns remain simple vectors.

  2. brendanf commented on Jun 6, 2018

    @brendanf

    With regards to rationale, my use case is for modeling functions like mlm() and mvabund::manyglm() which can take a matrix as the LHS of the model formula to represent a multivariate response; the data argument for those functions ideally has the matrix as a column.

  3. hadley commented on Jun 6, 2018

    @hadley
    MemberAuthor

    Because I think data frame and matrix columns are an important data structure for reasons that I can't fully articulate yet, but they would be useful in bigrquery (for representing nested, but not repeated, columns) and in data structures for visualisation.

    We absolutely do not want to flatten.

  4. romainfrancois commented on Jun 8, 2018

    @romainfrancois
    Member

    More from #419 that I'm closing now as a duplicate to this. Sorry for the noise:

    library(dplyr, warn.conflicts = FALSE)
    
    df <- data.frame(x1 = rep(1:3, times = 3), x2 = 1:9)
    df$x3 <- df %>% mutate(x3 = x2)
    as_tibble(df)
    #> Error: All columns in a tibble must be a 1d vector or a list:
    #> * Column `x3` is data.frame

    Related to tidyverse/dplyr#3630

    Some dplyr code can lead to a tibble with a data frame column and in that case printing and [ is broken:

    library(dplyr, warn.conflicts = FALSE)
    
    df <- data.frame(x1 = rep(1:3, times = 3), x2 = 1:9)
    df$x3 <- df %>% mutate(x3 = x2)
    
    d <- group_by(df, x1)
    # looks ok
    str(d)
    #> Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  9 obs. of  3 variables:
    #>  $ x1: int  1 2 3 1 2 3 1 2 3
    #>  $ x2: int  1 2 3 4 5 6 7 8 9
    #>  $ x3:'data.frame':  9 obs. of  3 variables:
    #>   ..$ x1: int  1 2 3 1 2 3 1 2 3
    #>   ..$ x2: int  1 2 3 4 5 6 7 8 9
    #>   ..$ x3: int  1 2 3 4 5 6 7 8 9
    #>  - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame':   3 obs. of  2 variables:
    #>   ..$ x1   : int  1 2 3
    #>   ..$ .rows:List of 3
    #>   .. ..$ : int  1 4 7
    #>   .. ..$ : int  2 5 8
    #>   .. ..$ : int  3 6 9
    
    # fails
    d
    #> Error in `[.data.frame`(X[[i]], ...): undefined columns selected
    
    # whereas
    as.data.frame(d)[1:3, ]
    #>   x1 x2 x3.x1 x3.x2 x3.x3
    #> 1  1  1     1     1     1
    #> 2  2  2     2     2     2
    #> 3  3  3     3     3     3
    
    # that's just weird. `[.grouped_df` is in dplyr
    d[1:3, ]
    #> # A tibble: 3 x 3
    #> # Groups:   x1 [3]
    #>      x1    x2 x3                          
    #>   <int> <int> <data.frame>                
    #> 1     1     1 c(1, 2, 3, 1, 2, 3, 1, 2, 3)
    #> 2     2     2 1:9                         
    #> 3     3     3 1:9
    
    # but [.tbl_df is in tibble and already does this
    class(d) <- c("tbl_df", "tbl", "data.frame")
    attr(d,"groups") <- NULL
    d[1:3, ]
    #> # A tibble: 3 x 3
    #>      x1    x2 x3                          
    #>   <int> <int> <data.frame>                
    #> 1     1     1 c(1, 2, 3, 1, 2, 3, 1, 2, 3)
    #> 2     2     2 1:9                         
    #> 3     3     3 1:9
    
    # and ... 
    d
    #> Error in `[.data.frame`(X[[i]], ...): undefined columns selected
  5. hadley commented on Jun 8, 2018

    @hadley
    MemberAuthor

    Ok, looks like we'll also need to make some basic fixes to the printing code.

    It's not clear to me how a df-col or matrix-col should print, so for now we just need some placeholder that at least doesn't error.

  6. romainfrancois commented on Jun 8, 2018

    @romainfrancois
    Member

    It's not just printing, [ also needs some attention:

    library(dplyr, warn.conflicts = FALSE)
    
    df <- data.frame(x1 = rep(1:3, times = 3), x2 = 1:9)
    df$x3 <- df %>% mutate(x3 = x2)
    
    d <- group_by(df, x1)
    str(d)
    #> Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  9 obs. of  3 variables:
    #>  $ x1: int  1 2 3 1 2 3 1 2 3
    #>  $ x2: int  1 2 3 4 5 6 7 8 9
    #>  $ x3:'data.frame':  9 obs. of  3 variables:
    #>   ..$ x1: int  1 2 3 1 2 3 1 2 3
    #>   ..$ x2: int  1 2 3 4 5 6 7 8 9
    #>   ..$ x3: int  1 2 3 4 5 6 7 8 9
    #>  - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame':   3 obs. of  2 variables:
    #>   ..$ x1   : int  1 2 3
    #>   ..$ .rows:List of 3
    #>   .. ..$ : int  1 4 7
    #>   .. ..$ : int  2 5 8
    #>   .. ..$ : int  3 6 9
    
    d2 <- d[1:2, ]
    str(d2)
    #> Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  2 obs. of  3 variables:
    #>  $ x1: int  1 2
    #>  $ x2: int  1 2
    #>  $ x3:'data.frame':  9 obs. of  2 variables:
    #>   ..$ x1: int  1 2 3 1 2 3 1 2 3
    #>   ..$ x2: int  1 2 3 4 5 6 7 8 9
    #>  - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame':   2 obs. of  2 variables:
    #>   ..$ x1   : int  1 2
    #>   ..$ .rows:List of 2
    #>   .. ..$ : int 1
    #>   .. ..$ : int 2
  7. hadley commented on Jun 8, 2018

    @hadley
    MemberAuthor

    Oh got it - [.tbl_df will need to supply additional missing arguments to [.

  8. hadley commented on Jun 8, 2018

    @hadley
    MemberAuthor

    Something like this, I think:

    subset_ROW <- function(x, i) {
      nd <- length(dim(x))
      if (nd <= 1L) {
        x[i]
      } else {
        dims <- rep(list(rlang::missing_arg()), nd - 1L)
        expr <- rlang::expr(x[i, !!!dims, drop = FALSE])
        rlang::eval_bare(expr)
      }
    }
    
    subset_ROW(1:10, 4:6)
    subset_ROW(array(1:10, c(10)), 4:6)
    subset_ROW(array(1:10, c(10, 1)), 4:6)
    subset_ROW(array(1:10, c(10, 1, 1)), 4:6)
  9. romainfrancois commented on Jun 8, 2018

    @romainfrancois
    Member

    Nice. I'll probably need to extract the part that generate the expression for internal use in dplyr, e.g. for what is bound to a symbol in the grouped case.

    Hopefully multi dimensional array columns won't be too much of a thing, matrix and data frame columns are already going to require some grasping.

  10. hadley commented on Jun 8, 2018

    @hadley
    MemberAuthor

    That's a good point. Rather than being so clever it might be better to do this:

    subset_ROW <- function(x, i) {
      nd <- length(dim(x))
      if (nd <= 1L) {
        x[i]
      } else if (nd == 2L) {
        x[i, , drop = FALSE]
      } else {
        stop("Only 0, 1, and 2d components are supported", call. = FALSE)
      }
    }
  11. krlmlr commented on Jun 29, 2018

    @krlmlr
    Member

    I have a working implementation locally, will push tomorrow. pillar is ready to handle the printing of these tibbles.

  12. banbh commented on Apr 2, 2019

    @banbh

    Would it be possible to extend support for matrix columns to mutate()? For example, tibble(a = 1:3, B = matrix(1:6, nrow = 3)) succeeds but tibble(a = 1:3) %>% mutate(B = matrix(1:6, nrow = 3)) fails with error "Error: Column B must be length 3 (the number of rows) or one, not 6". (tibble_2.0.1; dplyr_0.8.0.1).

  13. github-actions commented on Dec 8, 2020

    @github-actions
    Contributor

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

  14. locked and limited conversation to collaborators on Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions