extractRowRanges | R Documentation |
Extract Row Ranges by Pattern
extractRowRanges(
x,
pattern,
column = NULL,
starts = NULL,
startOffset = 1L,
stopOffset = 1L,
nameByMatch = FALSE,
nameColumnsByMatch = TRUE,
renumber = TRUE
)
x |
data frame or matrix or vector of character (representing e.g. the rows read from a text file) |
pattern |
pattern to be searched for, either in |
column |
name of column in which to search for pattern if
|
starts |
optional. Vector of indices representing the starts of the row
ranges to be extracted. This argument overrides |
startOffset |
row offset to be added to row number in which the pattern matches |
stopOffset |
row offset to be subtracted from row number in which the pattern matches |
nameByMatch |
logical. if |
nameColumnsByMatch |
if |
renumber |
if |
list of data frames or list of matrices or list of vectors of
character. Each list element represents one section of the input, found
between two consecutive matches of pattern
.
textLines <- c(
"Date,Value",
"1.1.,1",
"2.1.,2",
",",
"Date,Value",
"3.1.,3",
"4.1.,4",
",",
"Date,Value",
"5.1.,5",
"6.1.,6"
)
# Convert textLines to data frame. The function should be able to handle both.
(dataFrame <- read.table(text = textLines, sep = ",", stringsAsFactors = FALSE))
# stopOffset = 2L: skip empty line at the bottom of each sub-table
extractRowRanges(
textLines,
pattern = "Date",
stopOffset = 2L,
)
extractRowRanges(
dataFrame,
pattern = "Date",
column = "V1",
stopOffset = 2L
)
# Extract sections after a header line
# startOffset = 2L: skip empty line after header "topic: ..."
textLines <- c(
"topic: A",
"",
" a.1",
" a.2",
" a.3",
"topic: B",
"",
" b.1",
"topic: C",
"",
" c.1",
" c.2"
)
extractRowRanges(
textLines,
pattern = "topic",
startOffset = 2L,
nameByMatch = TRUE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.