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.

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.

References

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

Author

Ananda Mahto

Examples

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)
#> [[1]] #> ID V1 #> rowname.AB.01 1 68 #> rowname.BC.02 2 39 #> rowname.AD.03 3 1 #> rowname.CD.04 4 34 #> #> [[2]] #> ID V1 #> rowname.CD.07 7 14 #> rowname.BC.08 8 82 #> rowname.BC.09 9 59 #> rowname.BC.10 10 51 #> rowname.AC.11 11 97 #> rowname.BC.12 12 85 #>
getMyRows(dat1, c("AB", "AC"), -1:1, FALSE)
#> [[1]] #> ID V1 #> rowname.AB.01 1 68 #> rowname.BC.02 2 39 #> #> [[2]] #> ID V1 #> rowname.CD.04 4 34 #> rowname.AC.05 5 87 #> rowname.AC.06 6 43 #> #> [[3]] #> ID V1 #> rowname.AC.05 5 87 #> rowname.AC.06 6 43 #> rowname.CD.07 7 14 #> #> [[4]] #> ID V1 #> rowname.BC.10 10 51 #> rowname.AC.11 11 97 #> rowname.BC.12 12 85 #> #> [[5]] #> ID V1 #> rowname.BC.12 12 85 #> rowname.AB.13 13 21 #> rowname.CD.14 14 54 #> #> [[6]] #> ID V1 #> rowname.CD.14 14 54 #> rowname.AB.15 15 74 #> rowname.BC.16 16 7 #> #> [[7]] #> ID V1 #> rowname.BC.16 16 7 #> rowname.AB.17 17 73 #> rowname.CD.18 18 79 #> #> [[8]] #> ID V1 #> rowname.CD.18 18 79 #> rowname.AC.19 19 85 #> rowname.AD.20 20 37 #> #> [[9]] #> ID V1 #> rowname.AD.20 20 37 #> rowname.AC.21 21 89 #> rowname.CD.22 22 37 #> #> [[10]] #> ID V1 #> rowname.CD.23 23 34 #> rowname.AC.24 24 89 #> rowname.BD.25 25 44 #>
getMyRows(dat1, c("AB", "AC"), c(-1, 1), FALSE)
#> [[1]] #> ID V1 #> rowname.BC.02 2 39 #> #> [[2]] #> ID V1 #> rowname.CD.04 4 34 #> rowname.AC.06 6 43 #> #> [[3]] #> ID V1 #> rowname.AC.05 5 87 #> rowname.CD.07 7 14 #> #> [[4]] #> ID V1 #> rowname.BC.10 10 51 #> rowname.BC.12 12 85 #> #> [[5]] #> ID V1 #> rowname.BC.12 12 85 #> rowname.CD.14 14 54 #> #> [[6]] #> ID V1 #> rowname.CD.14 14 54 #> rowname.BC.16 16 7 #> #> [[7]] #> ID V1 #> rowname.BC.16 16 7 #> rowname.CD.18 18 79 #> #> [[8]] #> ID V1 #> rowname.CD.18 18 79 #> rowname.AD.20 20 37 #> #> [[9]] #> ID V1 #> rowname.AD.20 20 37 #> rowname.CD.22 22 37 #> #> [[10]] #> ID V1 #> rowname.CD.23 23 34 #> rowname.BD.25 25 44 #>