Closed MartinStjernman closed 4 years ago
This is expected. Unfortunately, many base R functions are not very welcoming to custom classes and attributes, and such behaviour is hard to overcome. There's a discussion about this in this vignette.
TL;DR, if you are lucky (and you are in this case), there some simplify
argument that you can set to FALSE
to preserve units
. The downside is that the result is provided in a list, and you may want to unlist that. But of course, unlist
will strip down units
too (in fact, unlist
is used internally with simplify=TRUE
), so you may want to unlist them carefully. In the linked vignette, we provide a function to do that in a general way (I hope), but is not very well tested.
If you find a better way, please let us know!
Thanks @Enchufa2 for this explanation.
I could use the workaround in more general cases but in specific situations where I have control over the calculations such that I know the resulting units after the aggregation I could of course "reset" the unit afterwards.
My example above, however, show that I cannot use:
`units(temp2$measure) <- with(ud_units, m^2)´
Error: cannot convert into m^2
Nor can I use:
temp2$measure <- set_units(temp2$measure, m^2)
Error: cannot convert into m^2
Apparently (and in line with the vignette you linked to), though still of class units
:
class(temp2$measure)
[1] "units"
temp2$measure
does not have any units
attributes that can be set using the above.
units(temp2$measure)
NULL
However, this seems to work:
temp2$measure <- temp2$measure*with(ud_units, m^2)
temp2
group measure 1 a -1.386437 [m^2] 2 b -2.848272 [m^2]
units(temp2$measure)
$numerator [1] "m" "m" $denominator character(0) attr(,"class") [1] "symbolic_units"
I conclude that I have to settle with this for the time being. Hopefully future developments will allow base R manipulations on quantity/units objects with preserved attributes and metadata. I personally does not have any obvious solution in mind but will let you know if I come up with one.
Thanks again for the help, I now close this issue
Hi all! I have stumbled on a problem: When trying to aggregate a
units
variable in a data.frame (or the like such as sf (simple feature)-objects; class 'sf' 'data.frame') usingaggregate()
the result is still aunits
variable (which is fine) but with "empty" unit ([]). And trying to set the unit on that variable result in an error (f ex Error: cannot covert into m^2). Here is a reproducible example:temp <- data.frame(group = factor(rep(letters[1:2], each=5)), measure = set_units(rnorm(10), m^2))
temp
temp2 <- aggregate(measure~group, data = temp, sum)
temp2
As can be seen
temp2$measure
has no unit but is still a units object. And trying to set unit does not work:units(temp2$measure) <- with(ud_units, m^2)
Is this expected behaviour or am I doing something wrong? It is a problem in my work since when working with simple features using the sf package and calculating area of polygons (
st_area(sf-object)
) the areas returned are units objects and when later wanting to do aggregation based on an attribute variable the summarized area has no unit and I cannot set it either. As my reproducible example shows this appears not to be a problem only within the sf package but something to do with the units and how they are handled byaggregate()
Any help is highly appreciated!
Thanks!
My sessionInfo():
sessionInfo()