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

Tidy allodb::equations in advance, not in new_equaions() #212

Open
maurolepore opened this issue Oct 9, 2021 · 0 comments
Open

Tidy allodb::equations in advance, not in new_equaions() #212

maurolepore opened this issue Oct 9, 2021 · 0 comments

Comments

@maurolepore
Copy link
Member

maurolepore commented Oct 9, 2021

The body of new_equations() starts cleaning up
the allodb::equations. If any such cleaning is
necessary, then it would be best to do it once
when we create allodb::equations rather than
every time a user calls new_equations(). This
should also help simplify new_equations(),
which already seems to do too much work.

Here is a piece of code that could go elsewhere

  ## open the equation table and get it in the right format ####
  equations_orig <- allodb::equations
  colnames(equations_orig) <- tolower(colnames(equations_orig))
  new_equations <- equations_orig

Here are the columns we could lowercase:

setdiff(
  names(allodb::equations),
  tolower(names(allodb::equations))
)
#> [1] "mat_C"           "min.temp_C"      "max.temp_C"      "dbh_unit_CF"    
#> [5] "output_units_CF"

Created on 2021-10-09 by the reprex package (v2.0.1)

--

Another piece of code that could go elsewhere

  suppressWarnings(new_equations$dbh_min_cm <-
    as.numeric(new_equations$dbh_min_cm))
  suppressWarnings(new_equations$dbh_max_cm <-
    as.numeric(new_equations$dbh_max_cm))
  suppressWarnings(new_equations$sample_size <-
    as.numeric(new_equations$sample_size))
  suppressWarnings(new_equations$dbh_unit_cf <-
    as.numeric(new_equations$dbh_unit_cf))
  suppressWarnings(new_equations$output_units_cf <-
    as.numeric(new_equations$output_units_cf))

This could be also done where we create allodb::equations, and maybe replaced by a simpler approach, e.g.:

# Setup
equations <- allodb::equations
names(equations) <- tolower(names(equations))

# This replaces the code above
to_numeric <- c("dbh_min_cm", "dbh_max_cm", "sample_size", "dbh_unit_cf", "output_units_cf")
equations[to_numeric] <- lapply(equations[to_numeric], \(x) suppressWarnings(as.numeric(x)))

unlist(lapply(equations[to_numeric], typeof))
#>      dbh_min_cm      dbh_max_cm     sample_size     dbh_unit_cf output_units_cf 
#>        "double"        "double"        "double"        "double"        "double"

Created on 2021-10-09 by the reprex package (v2.0.1)

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

1 participant