Code Monkey home page Code Monkey logo

Comments (1)

DavisVaughan avatar DavisVaughan commented on June 6, 2024

I think this may actually be a base R bug. I believe that all data.frames should have a names attribute. If there are 0 columns then it should be set to character(). The fact that that isn't the case here is odd.

attributes(as.data.frame(matrix(nrow = 0, ncol = 0)))
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#> integer(0)

attributes(data.frame())
#> $names
#> character(0)
#> 
#> $row.names
#> integer(0)
#> 
#> $class
#> [1] "data.frame"

Not having a names attribute can seriously break other parts of base R, making my guess that it is a base R bug feel more validated:

# Good OOB error
df <- data.frame()
df[1]
#> Error in `[.data.frame`(df, 1): undefined columns selected

# WTF is this
df <- as.data.frame(matrix(nrow = 0, ncol = 0))
df[1]
#> NULL
#> <0 rows> (or 0-length row.names)

In general dplyr works fine with the 2nd case, i.e. a well formed data frame


I think we should consider switching from names2() to names() in DataMask to try and catch this earlier on:

names_bindings <- chr_unserialise_unicode(names2(data))

i.e. something like

names <- names(data)

if (is.null(names)) {
  abort("Can't transform a data frame with `NULL` names.")
}

which would go nicely with our other names related checks there.

It did use names() in the past, but I switched to names2() here to supposedly fix this exact case. Supposedly not supporting NULL column names broke 2 packages and that was what I was out to fix, but that seems like a false alarm because current CRAN dplyr obviously already doesn't support NULL column names based on this issue. So the change above would just be giving a better error message for this invalid data frame.
#4896

from dplyr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.