
Split columns by parentheses, brackets, braces, or similar
Source:R/split-by-parens.R
split_by_parens.RdSummary 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()
)Arguments
- data
Data frame or matrix.
- cols
Columns to split, selected with tidyselect. Default is
everything(), restricted bycheck_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. Ifcolsis given, each column skipped causes a warning. Undercheck_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
.origincolumn naming the source? Default isFALSE. Requireskeep = FALSEand 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
See also
before_parens()andinside_parens()for the two parts of a string vector.tidyr::separate_wider_delim(), which is more general but does not recognize closing delimiters.
Examples
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()
#> # A tibble: 4 × 4
#> drone_x drone_sd selfpilot_x selfpilot_sd
#> <chr> <chr> <chr> <chr>
#> 1 0.09 0.21 0.19 0.13
#> 2 0.19 0.28 0.53 0.10
#> 3 0.62 0.16 0.50 0.11
#> 4 0.15 0.35 0.57 0.16
# Only some columns:
df1 |>
split_by_parens(cols = drone)
#> # A tibble: 4 × 3
#> drone_x drone_sd selfpilot
#> <chr> <chr> <chr>
#> 1 0.09 0.21 0.19 (0.13)
#> 2 0.19 0.28 0.53 (0.10)
#> 3 0.62 0.16 0.50 (0.11)
#> 4 0.15 0.35 0.57 (0.16)
# Longer format:
df1 |>
split_by_parens(transform = TRUE)
#> # A tibble: 8 × 3
#> .origin x sd
#> <chr> <chr> <chr>
#> 1 drone 0.09 0.21
#> 2 drone 0.19 0.28
#> 3 drone 0.62 0.16
#> 4 drone 0.15 0.35
#> 5 selfpilot 0.19 0.13
#> 6 selfpilot 0.53 0.10
#> 7 selfpilot 0.50 0.11
#> 8 selfpilot 0.57 0.16
# Other suffixes:
df1 |>
split_by_parens(end1 = "beta", end2 = "se")
#> # A tibble: 4 × 4
#> drone_beta drone_se selfpilot_beta selfpilot_se
#> <chr> <chr> <chr> <chr>
#> 1 0.09 0.21 0.19 0.13
#> 2 0.19 0.28 0.53 0.10
#> 3 0.62 0.16 0.50 0.11
#> 4 0.15 0.35 0.57 0.16
# 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")
#> # A tibble: 2 × 4
#> drone_x drone_sd selfpilot_x selfpilot_sd
#> <chr> <chr> <chr> <chr>
#> 1 0.09 0.21 0.19 0.13
#> 2 0.19 0.28 0.53 0.10
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("<", ">"))
#> # A tibble: 2 × 4
#> drone_x drone_sd selfpilot_x selfpilot_sd
#> <chr> <chr> <chr> <chr>
#> 1 0.09 0.21 0.19 0.13
#> 2 0.19 0.28 0.53 0.10