Learn R Programming

tidywater (version 0.7.0)

solvedose_ph_once: Apply `solvedose_ph` to a dataframe and create a new column with numeric dose

Description

This function allows solvedose_ph to be added to a piped data frame. Its output is a chemical dose in mg/L.

Usage

solvedose_ph_once(
  df,
  input_water = "defined_water",
  output_column = "dose_required",
  target_ph = NULL,
  chemical = NULL
)

Value

A data frame containing the original data frame and columns for target pH, chemical dosed, and required chemical dose.

Arguments

df

a data frame containing a water class column, which has already been computed using define_water_chain. The df may include a column with names for each of the chemicals being dosed.

input_water

name of the column of water class data to be used as the input. Default is "defined_water".

output_column

name of the output column storing doses in mg/L. Default is "dose_required".

target_ph

set a goal for pH using the function argument or a data frame column

chemical

select the chemical to be used to reach the desired pH using function argument or data frame column

Details

The data input comes from a `water` class column, initialized in define_water or balance_ions.

If the input data frame has column(s) named "target_ph" or "chemical", the function will use the column(s) as function argument(s). If these columns aren't present, specify "target_ph" or "chemical" as function arguments. The chemical names must match the chemical names as displayed in solvedose_ph. To see which chemicals can be dosed, see solvedose_ph.

For large datasets, using `fn_once` or `fn_chain` may take many minutes to run. These types of functions use the furrr package for the option to use parallel processing and speed things up. To initialize parallel processing, use `plan(multisession)` or `plan(multicore)` (depending on your operating system) prior to your piped code with the `fn_once` or `fn_chain` functions. Note, parallel processing is best used when your code block takes more than a minute to run, shorter run times will not benefit from parallel processing.

See Also

solvedose_ph

Examples

Run this code

library(purrr)
library(furrr)
library(tidyr)
library(dplyr)

example_df <- water_df %>%
  define_water_chain() %>%
  mutate(
    target_ph = 10,
    chemical = rep(c("naoh", "mgoh2"), 6)
  ) %>%
  solvedose_ph_once(input_water = "defined_water")

example_df <- water_df %>%
  define_water_chain() %>%
  solvedose_ph_once(input_water = "defined_water", target_ph = 8.8, chemical = "naoh")


example_df <- water_df %>%
  define_water_chain() %>%
  mutate(target_ph = seq(9, 10.1, .1)) %>%
  solvedose_ph_once(chemical = "naoh")

# \donttest{
# Initialize parallel processing
plan(multisession, workers = 2) # Remove the workers argument to use all available compute
example_df <- water_df %>%
  define_water_chain() %>%
  mutate(target_ph = seq(9, 10.1, .1)) %>%
  solvedose_ph_once(chemical = "naoh")

# Optional: explicitly close multisession processing
plan(sequential)
# }

Run the code above in your browser using DataLab