vctrs
vctrs copied to clipboard
Should miscellaneous attributes be preserved in `vec_c/rbind()`?
From https://github.com/tidyverse/dplyr/issues/6705 with columns during bind_rows()
library(vctrs)
x <- structure(1, label = "foo")
y <- structure(2, label = "bar")
vec_c(x, y)
#> [1] 1 2
x <- data_frame(x = x)
y <- data_frame(x = y)
vec_rbind(x, y)$x
#> [1] 1 2
Possibly retaining the attributes of the first input?
My heart says no, and that you should create a custom class that knows about label if you want this.
I vote for keeping the attributes intact. Or is there any easy way how to turn vectors of any class into vctrs vectors that know about custom attribute(s)? For my usecase, I want to store "label" attribute of columns in tibble and I don't want to lose it on every list_rbind() or bind_rows() call 😔. I tried to create vcrts "analogues" of doubles, integers, characters with the simple approach below, but failed miserably on factors...
new_vctr(x, label = label, class = "labelled", inherit_base_type = TRUE)