around: Return the rows around a specified position in a dataframe

View source: R/around.R

aroundR Documentation

Return the rows around a specified position in a dataframe

Description

This is a function like head or tail for dataframes which displays the values around a specified position.

Usage

around(dataframe, position, n = 3, return = "list", verbose = TRUE)

Arguments

dataframe

A dataframe.

position

A position/numeric vector of positions around which you want to examine the rows of a dataframe.

n

The number of adjacent positions around each position in position that you want to examine.

return

A character specifying "data.frame" or "list". If "list", then all the rows around each position are returned in an element of a list; if "data.frame", these are all returned in a single dataframe.

verbose

A logical input that defines whether or not to print a helpful message describing the rows that are being examined.

Value

The function returns a dataframe or a list depending on the input to return.

Author(s)

Edward Lavender

Examples


#### Example (1): A single position
# Define data
set.seed(1)
nrows <- 50
x <- runif(nrows, 1, 50)
y <- rnorm(nrows, x * 10 - 5, 60)
dat <- data.frame(x = x, y = y)
around(dat, 10, 3, "data.frame")

#### Example (2): Multiple positions, returned as a list
around(dat, c(10, 30, 39), 3, "list")

#### Example (3): Multiple positions returned as a dataframe
around(dat, c(10, 30, 39), 3, "data.frame")

#### Example (4): The function accounts for position at the start/end of the dataframe
around(dat, c(1, 10, nrow(dat)), 3)



edwardlavender/utils.add documentation built on Dec. 14, 2024, 8:11 a.m.