rows_to_names: Flexibly use row values as column names

Description Usage Arguments Value Examples

View source: R/rows_to_names.R

Description

Use a vector of integers, corresponding to row numbers, to use row values as column names. This is similar to janitor::row_to_names, except that it allows more flexibility in how the row values are handled, e.g. the number of rows to be included, preprocessing of row values, etc.

Usage

1
2
3
4
5
6
7
8
9
rows_to_names(
  .data,
  name_fn = NULL,
  ...,
  row_nums = 1,
  sep = "<<=>>",
  drop_rows = TRUE,
  merge_current = FALSE
)

Arguments

.data

any "square" data, i.e. data frames and matrices.

name_fn

a function to be applied to each collection of rows.

...

additional arguments to be passed to name_fn.

row_nums

an integer or vector of integers corresponding to rows.

sep

a string used to separate combined rows.

drop_rows

a logical indicating if header rows should be dropped.

merge_current

a logical indicating if new headers should be merged with existing headers.

Value

An object of the same type as .data.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Example data.
df <- as.data.frame(list(c("h11x", "h12y", "1"), c("h21x", "h22y", "2")))

# The first row is used as column names by default.
rows_to_names(df)

# Choose more rows using an integer vector.
rows_to_names(df, row_nums = 1:2)

# Set merge_current to TRUE to include existing column names. The following
# leads to the same results as above.
rows_to_names(rows_to_names(df), merge_current = TRUE)

# Use name_fn to process row values before they are combined.
rows_to_names(
    df,
    name_fn = sub,
    pattern = "x|y",
    replacement = "",
    row_nums = 1:2
)

gershomtripp/gttoolkit documentation built on Dec. 20, 2021, 10:41 a.m.