Skip to content
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

arrange() does not preserves the attributes of hms class variable #7624

Closed
pangchaoran opened this issue Jan 7, 2025 · 1 comment
Closed

Comments

@pangchaoran
Copy link

In the data frame returned by arrange(), the attribute of hms class variable disappears, though the attributes of other variables are preserved.

df1 <- tibble(var1=1:5, var2=hms::hms(1:5))
attr(df1$var1,'label') <- "variable 1"
attr(df1$var2,'label') <- "variable 2"
df2 <- df1 |> arrange(var1)
attr(df2$var1,'label')
attr(df2$var2,'label')
@DavisVaughan
Copy link
Member

DavisVaughan commented Jan 7, 2025

While maybe a little confusing, I don't think this is a bug. With classed objects, like hms, our general goal is to make sure that we retain only the attributes known and managed by the class. In the case of hms, that's the class attribute and the units attribute. It is an impossible problem to ensure that the class is kept in a "correct" state if we always copy over all attributes, including "unknown" ones not managed by the class.

We don't make any promises about other attributes and you really can't expect them to be kept.

With "bare" vectors that don't have any classes, we've made an exception to try and drag along attributes that might be attached, but this is mostly a historical thing and not something we encourage.

In general if you want to retain extra attributes you should create your own S3 class that "knows" about that attribute and can restore them (like with a vctrs::vec_restore() method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants