R/first.R

#' Identify the First Elements of Series of Repeated Values
#'
#' Identify the first elements of series of repeated values.
#' @param x
#'   A vector whose values will be explored for series of repeated values,
#'   can be character, numeric, or factor.
#' @return
#'   An integer vector the same length as \code{x}, with a 1 for every element
#'   that is different than the one before it, and a 0 for every element
#'   that is the same as the one before it.
#' @references
#'   This is a copy of the \code{first} function from the
#'   \href{https://github.com/JVAdams/jvamisc}{[jvamisc]} package.
#' @export
#' @examples
#' first(c(1, 2, 1, 2, 2, 1, 1, 3))

first <- function(x) {
  l <- length(x)
  c(1, 1-(x[-1]==x[-l]))
}
JVAdams/artiFISHal documentation built on May 7, 2019, 10:14 a.m.