getMyRows: Get Rows Before and After a Specific Value or Rowname

Description Usage Arguments Value Author(s) References Examples

View source: R/getMyRows.R

Description

Extracts (possibly overlapping) rows provided both a pattern or index position to match and a range specifying how many rows before and after the matched row.

Usage

1
getMyRows(data, pattern, range, isNumeric = TRUE)

Arguments

data

The input data.frame.

pattern

Either the pattern to match in the rownames or the numeric position of the initial rows.

range

A vector indicating the range of rows you want to extract (in the form of -2:3). In this case, it would extract two rows before and three rows after the matched index or pattern. Alternatively, a specific vector (instead of a range) can be supplied, as in c(-1, 1) in which case only the previous and next row will be returned, but not the row itself.

isNumeric

Logical. Is pattern a numeric vector of row positions (isNumeri = TRUE) or is it a string value that needs to be matched against the rownames? Defaults to TRUE.

Value

A list of data.frames with the relevant rows extracted.

Author(s)

Ananda Mahto

References

http://stackoverflow.com/a/13155669/1270695

Examples

1
2
3
4
5
6
7
8
9
set.seed(1)
dat1 <- data.frame(ID = 1:25, V1 = sample(100, 25, replace = TRUE))
rownames(dat1) <- paste("rowname", sample(apply(combn(LETTERS[1:4], 2),
                        2, paste, collapse = ""),
                        25, replace = TRUE),
                        sprintf("%02d", 1:25), sep = ".")
getMyRows(dat1, c(2, 10), -3:2)
getMyRows(dat1, c("AB", "AC"), -1:1, FALSE)
getMyRows(dat1, c("AB", "AC"), c(-1, 1), FALSE)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.