Learn R Programming

scrutiny (version 1.0.0)

split_by_parens: Split columns by parentheses, brackets, braces, or similar

Description

Summary statistics are often reported like "2.65 (0.27)". split_by_parens() splits such columns into the part before the parentheses and the part inside them. By default, it splits every column that contains the delimiters. Set transform to TRUE to pivot the result into a longer format.

Usage

split_by_parens(
  data,
  cols = everything(),
  check_delim = TRUE,
  keep = FALSE,
  transform = FALSE,
  delim = "parens",
  end1 = "x",
  end2 = "sd",
  check_sep = deprecated(),
  sep = deprecated()
)

Value

An ungrouped data frame: a tibble unless data is a plain data frame and transform is FALSE.

Arguments

data

Data frame or matrix.

cols

Columns to split, selected with tidyselect. Default is everything(), restricted by check_delim.

check_delim

Logical. If TRUE (the default), columns whose values do not all contain the delimiters are skipped. Missing values, blank strings, "NA", and "NaN" don't count. If cols is given, each column skipped causes a warning. Under check_delim = FALSE, so does each column split although it lacks the delimiters, unless all its values are missing.

keep

Logical. Should the original columns be kept? Default is FALSE.

transform

Logical. Should the output be pivoted into a longer format, with one column per part and an .origin column naming the source? Default is FALSE. Requires keep = FALSE and every column to be split.

delim

String. The pair of delimiters around the second part: "parens" (the default), "brackets", or "braces", or two custom strings, matched literally.

end1, end2

Strings. Suffixes of the two columns that a split column produces. Defaults are "x" and "sd".

check_sep, sep

[Deprecated] The old names of check_delim and delim.

See Also

  • before_parens() and inside_parens() for the two parts of a string vector.

  • tidyr::separate_wider_delim(), which is more general but does not recognize closing delimiters.

Examples

Run this code
df1 <- tibble::tribble(
  ~drone,           ~selfpilot,
  "0.09 (0.21)",    "0.19 (0.13)",
  "0.19 (0.28)",    "0.53 (0.10)",
  "0.62 (0.16)",    "0.50 (0.11)",
  "0.15 (0.35)",    "0.57 (0.16)",
)

df1 |>
  split_by_parens()

# Only some columns:
df1 |>
  split_by_parens(cols = drone)

# Longer format:
df1 |>
  split_by_parens(transform = TRUE)

# Other suffixes:
df1 |>
  split_by_parens(end1 = "beta", end2 = "se")

# Other delimiters:
df2 <- tibble::tribble(
  ~drone,           ~selfpilot,
  "0.09 [0.21]",    "0.19 [0.13]",
  "0.19 [0.28]",    "0.53 [0.10]",
)

df2 |>
  split_by_parens(delim = "brackets")

df3 <- tibble::tribble(
  ~drone,           ~selfpilot,
  "0.09 <0.21>",    "0.19 <0.13>",
  "0.19 <0.28>",    "0.53 <0.10>",
)

df3 |>
  split_by_parens(delim = c("<", ">"))

Run the code above in your browser using DataLab