Hi

The implementation answer is that grid::unit() calls as.numeric() on 'x', which drops names.

The design answer is that having names on units was (naively) not considered in the original design.

The obstacle is that all functions that manipulate units would need to be made "names-aware". That might be tricky for more complex units like ...

max(unit(1:3, "in"))

... though I guess the behaviour of numeric vectors could provide a role-model.

Paul

On 28/08/2025 2:08 am, David Firth via R-devel wrote:
I am asking here first, rather than filing a feature request directly.  It 
concerns a function in the grid package, maintained by R-core.

Is there a reason why grid::unit(x, units) does not preserve the names (if such 
exist) from vector x?

I have a use-case where that would be helpful.  But maybe there is some 
obstacle that I have not thought of?

Current behaviour seems to be:

x <- 7:9
names(x) <- letters[1:3]
x
a b c
7 8 9
z <- grid::unit(x, "native")
z
[1] 7native 8native 9native
str(z)
  'simpleUnit' num [1:3] 7native 8native 9native
  - attr(*, "unit")= int 4
class(z)
[1] "simpleUnit" "unit"       "unit_v2"

## Now assign names
names(z) <- names(x)
z
[1] 7native 8native 9native
names(z)
[1] "a" "b" "c"
str(z)
  'simpleUnit' Named num [1:3] 7native 8native 9native
  - attr(*, "unit")= int 4
  - attr(*, "names")= chr [1:3] "a" "b" "c"

I would welcome any comments or suggestions.

Thanks -- David

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

--
Dr Paul Murrell (he/him)
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland 1142, New Zealand
64 9 3737599 x85392
p...@stat.auckland.ac.nz
www.stat.auckland.ac.nz/~paul/

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to