R/nth.R

Defines functions nth

Documented in nth

#' @title Extract the nth value from a vector
#' @description Get the value from a vector with its position.
#' @param v A vector
#' @param n A single integer specifying the position. Default uses \code{1}.
#' Negative integers index from the end
#'  (i.e. -1L will return the last value in the vector).
#'  If a double is supplied, it will be silently truncated.
#' @return A single value.
#' @examples
#'
#' x = 1:10
#' nth(x, 1)
#' nth(x, 5)
#' nth(x, -2)
#'
#' @export

nth = function(v,n = 1){
  n <- trunc(n)
  ifelse(
    n >= 0,
    v[[n]],
    v[[length(v)+n+1]]
  )
}

Try the tidyft package in your browser

Any scripts or data that you put into this service are public.

tidyft documentation built on Sept. 30, 2024, 9:40 a.m.