-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
what *is* a vector? #1955
Comments
There is the storage type and there is the semantic type (a combination of interface and semantics). rlang is about the storage type, vctrs is about semantics. We've decided that S3 subclasses must explicitly inherit from a base vector/list class to be considered as such, even if they have vector/list storage. For instance, in the vctrs worldview an S3 model is a scalar and not a list, even though it has list storage. |
FWIW
|
As Lionel said, this distinction allows us to say that output from But a This rule about what an explicit |
In particular, a good example here is the
Another good example are the
|
Thank you all for the very clear and thoughtful responses! Following the details section in Vector Checks (which should be more discoverable, imo its really great writing!) this issue can be addressed by simply adding a new Overall what I take away is that the comparison between rlang and vctrs should be between the library(spdep)
library(vctrs)
# create listw object
nb <- cell2nb(10, 10)
listw <- nb2listw(nb)
# these tests should be the same
rlang::is_bare_list(nb)
#> [1] FALSE
rlang::is_bare_vector(nb)
#> [1] FALSE
vctrs::obj_is_list(nb)
#> [1] FALSE
vctrs::obj_is_vector(nb)
#> [1] FALSE
# tell {vctrs} that nb _is_ a vector
vec_proxy.nb <- function(x, ...) {
unclass(x)
}
# do these tests with {vctrs} again and see it is now vector
# but still not list
vctrs::obj_is_list(nb)
#> [1] FALSE
vctrs::obj_is_vector(nb)
#> [1] TRUE
# give a format method for the record
format.swm_rcrd <- function(x, ...) {
nbs <- field(x, "neighbours")
card <- spdep::card(nbs)
out <- paste("(", vapply(nbs, toString, character(1)), ")", sep = "")
out[which(card == 0)] <- NA
out
}
# try and create a record
x <- new_rcrd(listw, class = "swm_rcrd")
head(x)
#> <swm_rcrd[6]>
#> [1] (2, 11) (1, 3, 12) (2, 4, 13) (3, 5, 14) (4, 6, 15) (5, 7, 16) Created on 2024-10-23 with reprex v2.1.0 |
This is somewhat of a philosophical question but with real consequences—so I apologize for it winding and curving!
TL;DR
The
nb
class is a list with attributes and no explicitlist
class.This is how the following packages see it
Background
One thing that has been bothering me since 2021 is that the
nb
andlistw
classes from the spdep cannot be easily integrated into the tidyverse.The
nb
class object is a ragged array stored in a list. A list is a vector and thus can work with vctrs and the tidyverse in general. However, thenb
class object does not have thelist
class explicitly added. There is disagreement across base R, rlang, and vctrs about what constitutes a vector and a list.Motivation
The
rcrd
class fromvctrs
provides a nice opportunity to be able to embed thelistw
class into the tidyverse workflow in a much more seamless way than has been possible in the past.I am quite interested in thinking through how I can make spatial statistics more accessible to the R ecosystem and this is a big part of it. I have a package sfdep which provides tidyverse compatibility by way of partitioning these two component lists
neighbours
andweights
as two separate columns in a dataframe. Ideally, it would be one as it can become out of sync.Question
What constitutes a
list
and avector
in vctrs and should there be agreement betweenrlang
andvctrs
as to what this is?Additionally, do you all have guidance as how one can address this? FWIW, I am not the author or maintainer of
{spdep}
and adding thelist
subclass is out of question as demonstrated in r-spatial/spdep#59.Reprex
Created on 2024-10-22 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: