new_value: Generate New Reference Value

View source: R/new-value.R

new_valueR Documentation

Generate New Reference Value

Description

Generate a new reference value for a vector.

Usage

new_value(x, ..., obs_only = NULL)

Arguments

x

The object to generate the reference value from.

...

These dots are for future extensions and must be empty.

obs_only

A flag specifying whether to only use observed values.

Details

By default the reference value for double vectors is the mean, unless obs_only = TRUE, in which case its the median of the unique values. For integer vectors it's the floored mean unless obs_only = TRUE, in which case it's also the median of the unique values. For character vectors it's the minimum of the most common values while for factors it's the first level. Ordered factors, Dates, times (hms), POSIXct and logical vectors are treated like integers. The factor levels and time zone are preserved.

Value

A scalar of the same class as the object.

See Also

xnew_value() and new_seq().

Examples

# the reference value for objects of class numeric is the mean
new_value(c(1, 4))
# unless obs_only = TRUE, in which case its the median of the unique values
new_value(c(1, 4), obs_only = TRUE)

# for integer objects it's the floored mean
new_value(c(1L, 4L))

# for character objects it's the minimum of the most common values
new_value(c("a", "b", "c", "c", "b"))

# for factors its the first level and the factor levels are preserved
new_value(factor(c("a", "b", "c", "c"), levels = c("b", "a", "g")))

# other classes are treated like integers
new_value(ordered(c("a", "b", "c", "c"), levels = c("b", "a", "g")))
new_value(as.Date(c("2000-01-01", "2000-01-04")))
new_value(hms::as_hms(c("00:00:01", "00:00:04")))
new_value(as.POSIXct(c("2000-01-01 00:00:01", "2000-01-01 00:00:04")),
  tzone = "PST8PDT")
new_value(c(TRUE, FALSE, TRUE))


poissonconsulting/newdata documentation built on Jan. 18, 2024, 1:30 a.m.