
Organize a Raw Excel Range into a data.frame, matrix, list or table
Source:R/xlGetRange.R
xlParseRange.RdTurns the raw output of xlGetRange into a proper R object.
Handles a single range as well as a multi-area selection, with optional
header handling and automatic per-column type conversion.
Usage
xlParseRange(
x,
output = c("data.frame", "matrix", "list", "table"),
header = FALSE,
convert = TRUE,
stringsAsFactors = FALSE
)Arguments
- x
an
"XLRange"object, or a list of them (multi-area), as returned byxlGetRange.- output
target structure. One of
"data.frame","matrix","list","table".- header
logical; if
TRUE, the first row is treated as a header (column names). Ignored for"table", which always uses the first row / column as names.- convert
logical; if
TRUE(default), each column is run through.xlConvert()for automatic numeric / Date detection.- stringsAsFactors
logical; passed to
data.framefor the"data.frame"target. Defaults toFALSE.
Value
A data.frame, matrix, list, or a matrix with
dimnames (for "table"). For a multi-area "list", the
returned list carries an address attribute.
Details
Single range. The requested structure determines the result:
"data.frame"with
header = TRUEthe first row supplies the column names; otherwise names areV1, V2, ...."matrix"the whole range as a matrix. When
convertisTRUEand every column is numeric, a numeric matrix results; otherwise a character matrix."list"one component per column (named by the header when
header = TRUE)."table"first column becomes row names, first row becomes column names, and the top-left corner cell is discarded. The result is a matrix with
dimnames(not atableobject). Requires at least 2 rows and 2 columns.
Multiple areas. When x is a list of "XLRange" blocks
(disjoint selection), only two targets are supported:
"list"each area is parsed as its own matrix; the result is a list of matrices.
"data.frame"each area is parsed as a data.frame and the areas are bound column-wise into a single data.frame. Columns of unequal length are padded with
NAup to the longest column, and duplicate column names are disambiguated withmake.unique.
Any other target raises an error for multi-area input.
Examples
if (FALSE) { # \dontrun{
r <- xlGetRange(xl)
xlParseRange(r, output = "data.frame", header = TRUE)
xlParseRange(r, output = "matrix")
xlParseRange(r, output = "table") # first col = rownames, first row = colnames
} # }