Return a single value from a column of a dataframe using the method specified
Arguments
- data
A dataframe which has a column that matches (at least partially) colname
- method
How should the value to be returned be selected? Current options include "most_common_value", where the most common input uncertainty value will be returned and "only_value" where all input values must be the same in data; if they are, this value will be returned. Otherwise, an error will be generated.
- colname
Column name as a string in
data
. Note that partial matching and regular expressions can be used.- group
Column name as a string in
data
used to group the data before calculating the input value to use. Defaults to NULL.
Value
A value of the same type as data[, colname]
if group
is NULL, or a data.frame if group
is specified.
Details
Note that this function was created intially to return a value to use as the input uncertainty, but it should be generalizable to pulling a value from a column in any data frame using the method specified.
Examples
dfr <- data.frame(
"year" = 1:5,
"value" = c(2, 2, 2, 3, 3),
"se_log" = 0.2
)
SSMSE:::get_input_value(
data = dfr, method = "most_common_value", colname = "se_log",
group = "value"
)
#> value se_log
#> 1 2 0.2
#> 2 3 0.2
SSMSE:::get_input_value(data = dfr, method = "most_common_value", colname = "value")
#> [1] 2
SSMSE:::get_input_value(data = dfr, method = "only_value", colname = "se_log")
#> [1] 0.2
# generates an error:
# SSMSE:::get_input_value(data = dfr, method = "only_value", colname = "value")