R/last.R

Defines functions last_column last_row last

Documented in last last_column last_row

#' Select character from last
#'
#' @param x vector
#' @param n If missing, the last element will be used.
#' 
#'
#' @return last element
#' @export
#'
#' @examples
#' letters |> last()
#' letters |> last(1:2)
last <- function(x,n){
    if (missing(n)){
        x[length(x)]
    }else{
        rev(x)[n] |> rev()
    }
}




#' Select dataframe row from last
#'
#' @param x dataframe
#' @param n If missing, the last element will be used.
#' 
#'
#' @return last row
#' @export
#'
#' @examples
#' mtcars |> last_row()
#' mtcars |> last_row(1:2)
last_row <- function(x,n){
    if (missing(n)){
        x[nrow(x),]
    }else{
        rown <- rev(seq(1:nrow(x)))[n]
        x[rown,]
    }
}

#' Select dataframe column from last
#'
#' @param x dataframe
#' @param n If missing, the last element will be used.
#' 
#'
#' @return last column
#' @export
#'
#' @examples
#' mtcars |> last_column()
#' mtcars |> last_column(1:2)
last_column <- function(x,n){
    if (missing(n)){
        x[,ncol(x)]
    }else{
        rev(x)[,n] |> rev()
    }
}

Try the do package in your browser

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

do documentation built on Aug. 3, 2021, 5:06 p.m.