Closed billdenney closed 1 year ago
Thanks for the report, yes, we want to keep names.
While thinking about this, I had an idea for a potential edge case. For normal R numeric vectors, they take the names of the first argument argument in vector/vector operations if they are the same length and the length of the longer argument if the lengths differ.
c(A=1, B=2)/c(D=3, E=4)
#> A B
#> 0.3333333 0.5000000
c(A=1)/c(D=3, E=4)
#> D E
#> 0.3333333 0.2500000
c(A=1, B=2, C=3, D=4)/c(D=3, E=4)
#> A B C D
#> 0.3333333 0.5000000 1.0000000 1.0000000
c(D=3, E=4)/c(A=1, B=2, C=3, D=4)
#> A B C D
#> 3 2 1 1
Created on 2022-02-23 by the reprex package (v2.0.1)
Interesting. It makes sense, since the shorter one is the one that gets recycled.
library(units)
# udunits database from /usr/share/xml/udunits/udunits2.xml
c(a = 3, b = 4) |> set_units(m)
# Units: [m]
# a b
# 3 4
c(a = 3, b = 4) |> set_units(m) |> set_units(km)
# Units: [km]
# [1] 0.003 0.004
c(a = 3, b = 4) |> set_units(m) |> set_units(km) |> setNames(c("a", "b"))
# Units: [km]
# a b
# 0.003 0.004
It seems units get lost when unit conversion takes place, not when setting them.
In the example below,
names()
are lost when the units cancel, but they are retained when units do not cancel. I think that the preferred behavior would keep thenames()
.Created on 2022-02-23 by the reprex package (v2.0.1)