Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed units when using a pipe operator #220

Closed
LS31 opened this issue Dec 22, 2019 · 2 comments
Closed

Mixed units when using a pipe operator #220

LS31 opened this issue Dec 22, 2019 · 2 comments

Comments

@LS31
Copy link

LS31 commented Dec 22, 2019

I discovered the units package yesterday and I really like the idea of explicitly attributing units to data. I tried to incorporate it in a function, but ran into unexpected behaviour. When using a pipe operator the units seem to get mixed up.

library(magrittr)

calculate_bmi_A <- function(weight, height) {
  weight / ((height / 100) ^ 2) %>%
    units::set_units("kg1 m-2", mode = "standard")
}

calculate_bmi_B <- function(weight, height) {
  x = weight / ((height / 100) ^ 2)
  units::set_units(x, "kg1 m-2", mode = "standard")
}

calculate_bmi_A(120, 180)
#> 37.03704 [m^2/kg]
calculate_bmi_B(120, 180)
#> 37.03704 [kg/m^2]

I can not really say if this is related to issue #86

Is this expected behaviour and am I missing something? Or is it a bug? Thanks!

@edzer
Copy link
Member

edzer commented Dec 22, 2019

Looks like an operator precedence issue:

weight = 120; height = 180
> weight / ((height / 100) ^ 2) %>% units::set_units("kg1 m-2", mode = "standard")
37.03704 [m^2/kg]
> (weight / ((height / 100) ^ 2)) %>% units::set_units("kg1 m-2", mode = "standard")
37.03704 [kg/m^2]

@LS31
Copy link
Author

LS31 commented Dec 22, 2019

I see. So not a bug in units, but unexpected expected behaviour :) Thanks for the quick reply.

@LS31 LS31 closed this as completed Dec 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants