
Promote a mis-placed header row and drop leading metadata rows
Source:R/data_check_helpers.R
data_promote_header_row.RdWhen a banner / blank / units / repeated-label row sits ABOVE the real header,
the reader takes that top row as the header (inventing ...N names, or spreading
one label – CDA merged across 110 columns – into CDA...1 ... CDA...110). This
finds the true header among the first few rows via .detect_header_row(),
promotes it to the column names, drops it and everything above, and re-types the
freed columns. It is the inverse of data_strip_qualtrics_header() (which
strips junk rows BELOW a correct header).
Arguments
- df
the data.frame as normally read (reader-assigned names)
- raw_rows
optional list of character vectors: the first rows of the file read WITHOUT a header, so the reader-swallowed header row is visible. When supplied it is the authoritative scan window.
- max_scan
how many leading rows to consider as metadata above the header
Value
a list with df (possibly re-headed), promoted (1-based count of
metadata rows removed above the header, 0 = unchanged), and stripped
(character vectors of those removed rows, for reporting).
Details
The input must be read so that the real header is still a DATA row (i.e. read
headerless, or with the reader's own header as row-0 metadata). Pass the rows
via raw_rows (the file re-read headerless as character) so the detector can
see the row the reader swallowed as the header; without it the function falls
back to treating df's own first rows as the scan window.
Examples
# a title row above the real header, read as the header
df <- data.frame(`Stroop study export` = c("id", "1", "2", "3"),
X = c("rt", "512", "640", "498"), check.names = FALSE)
res <- data_promote_header_row(df)
res$promoted
#> [1] 1
res$df
#> id rt
#> 1 1 512
#> 2 2 640
#> 3 3 498