r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

Unit not preserved when computing standard deviation #306

Closed loicdtx closed 2 years ago

loicdtx commented 2 years ago

When reducing a vector with the sd function the unit is dropped. Is that the expected behavior?

library(units)
a <- rnorm(200, mean = 23, sd = 2.1)
b <- set_units(a, m^2)
mean(b) # 23.07854 [m^2]
sd(b) # [1] 2.199572
edzer commented 2 years ago

Yes, sd is a function, not a generic, so we can't provide a method for units objects.

loicdtx commented 2 years ago

Understood, thanks @edzer