This function extracts specific rows from a data frame along with their context, based on a given range. It can take either a vector of row numbers or a boolean vector as input. Additionally, the function allows specifying the direction of the context (above, below, or both) and marks each row as either targeted or context.
Arguments
- data
A data frame from which rows and their context will be extracted.
- input
A numeric vector of row numbers or a logical vector. If a logical vector is given, the function will extract rows where the vector is TRUE.
- context
An integer specifying the number of rows above and/or below the target row to include as context. Default is 1.
- direction
A character string specifying the direction of context to include. Valid options are "up" for rows above, "down" for rows below, and "both" for both directions. Default is "both".
Value
A dataframe containing the targeted rows and their contextual rows. An additional column '.RowType' indicates whether it is a row given as input ("->") or part of the context ("").
Examples
# Example dataframe
data <- data.frame(matrix(rnorm(100), ncol = 5))
# Get context for rows 8 and 15 (using row numbers)
result <- controws(data, c(8, 15), 1, "both")
result
#> .RowType X1 X2 X3 X4 X5
#> 7 -0.784397340 0.06832755 0.8182838 -1.1846876 2.3747402
#> 8 -> -1.249873383 -0.26771827 -1.6531114 -0.4385380 1.9277134
#> 9 -0.130581839 2.68432115 0.3771345 -0.3177573 2.3153621
#> 14 -0.003058868 -0.98509056 1.2129737 0.5657584 0.5354938
#> 15 -> -1.459466080 1.62422637 0.6222768 2.0730449 -0.7011515
#> 16 -0.834825382 0.24266819 1.0252649 -0.4599022 -1.9308205
