View source: R/differs_from_previous.R
differs_from_previous | R Documentation |
Finds values, or indices of values, that differ from the previous value by some threshold(s).
Operates with both a positive and a negative threshold.
Depending on `direction`
, it checks if the difference to the previous value is:
greater than or equal to the positive threshold.
less than or equal to the negative threshold.
differs_from_previous(
data,
col = NULL,
threshold = NULL,
direction = "both",
return_index = FALSE,
include_first = FALSE,
handle_na = "ignore",
factor_conversion_warning = TRUE
)
data |
N.B. If checking a N.B. If |
col |
Name of column to find values that differ in. Used when |
threshold |
Threshold to check difference to previous value to.
NULLChecks if the value is different from the previous value. Ignores N.B. Works for both numeric and character vectors. Numeric scalarPositive number. Negative threshold is the negated number. N.B. Only works for numeric vectors. Numeric vector with length 2Given as Negative threshold must be a negative number and positive threshold must be a positive number. N.B. Only works for numeric vectors. |
direction |
bothChecks whether the difference to the previous value is
positiveChecks whether the difference to the previous value is
negativeChecks whether the difference to the previous value is
|
return_index |
Return indices of values that differ. (Logical) |
include_first |
Whether to include the first element of the vector in the output. (Logical) |
handle_na |
How to handle "ignore"Removes the "as_element"Treats all Numeric scalarA numeric value to replace |
factor_conversion_warning |
Whether to throw a warning when converting a |
vector
with either the differing values or the indices of the differing values.
N.B. If `data`
is a grouped data.frame
,
the output is a list
of vector
s
with the differing values. The names are based on the group indices
(see dplyr::group_indices()
).
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
Other l_starts tools:
find_missing_starts()
,
find_starts()
,
group_factor()
,
group()
# Attach packages
library(groupdata2)
# Create a data frame
df <- data.frame(
"a" = factor(c("a", "a", "b", "b", "c", "c")),
"n" = c(1, 3, 6, 2, 2, 4)
)
# Get differing values in column 'a' with no threshold.
# This will simply check, if it is different to the previous value or not.
differs_from_previous(df, col = "a")
# Get indices of differing values in column 'a' with no threshold.
differs_from_previous(df, col = "a", return_index = TRUE)
# Get values, that are 2 or more greater than the previous value
differs_from_previous(df, col = "n", threshold = 2, direction = "positive")
# Get values, that are 4 or more less than the previous value
differs_from_previous(df, col = "n", threshold = 4, direction = "negative")
# Get values, that are either 2 or more greater than the previous value
# or 4 or more less than the previous value
differs_from_previous(df, col = "n", threshold = c(-4, 2), direction = "both")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.