I’ve been confused several times, so here is the question for others who might stumble upon the same problem.
Consider this grid unit vector,
a = unit(1:3, c("cm", "in", "npc"))
I want to replace some elements with new values. A natural approach would be
a[1] = unit(2,"pt") a # [1] 2cm 2in 3npc
Something went wrong: only the numerical value changed, not the unit. What for? What to do?
Edit: As indicated in one of the answers below, such units are simply numeric vectors with attributes. However, their offspring unit.arithmetic and unit.list should also be considered as a completely general solution (for example, use ggplot when setting sizes of object panels). Consider this unit vector,
(b = a + unit(1, "npc")) # [1] 1cm+1npc 2in+1npc 3npc+1npc # [1] "unit.arithmetic" "unit"
Replacing a particular element is now more complex since they are no longer atomic.
source share