vctrs icon indicating copy to clipboard operation
vctrs copied to clipboard

Should miscellaneous attributes be preserved in `vec_c/rbind()`?

Open DavisVaughan opened this issue 2 years ago • 1 comments

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.

DavisVaughan avatar Feb 09 '23 22:02 DavisVaughan

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)

netique avatar Oct 26 '24 15:10 netique