Merge flextable cells vertically when consecutive cells have identical values. Text of formatted values are used to compare values if available.

Two options are available, either a column-by-column algorithm or an algorithm where the combinations of these columns are used once for all target columns.

merge_v(x, j = NULL, target = NULL, part = "body", combine = FALSE)

Arguments

x

flextable object

j

column to used to find consecutive values to be merged. Columns from orignal dataset can also be used.

target

columns names where cells have to be merged.

part

partname of the table where merge has to be done.

combine

If the value is TRUE, the columns defined by j will be combined into a single column/value and the consecutive values of this result will be used. Otherwise, the columns are inspected one by one to perform cell merges.

Illustrations

See also

Other flextable merging function: merge_at(), merge_h_range(), merge_h(), merge_none()

Examples

ft_merge <- flextable(mtcars)
ft_merge <- merge_v(ft_merge, j = c("gear", "carb"))
ft_merge
#> a flextable object.
#> col_keys: `mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb` 
#> header has 1 row(s) 
#> body has 32 row(s) 
#> original dataset sample: 
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

data_ex <- structure(list(srdr_id = c(
  "175124", "175124", "172525", "172525",
  "172545", "172545", "172609", "172609", "172609"
), substances = c(
  "alcohol",
  "alcohol", "alcohol", "alcohol", "cannabis",
  "cannabis", "alcohol\n cannabis\n other drugs",
  "alcohol\n cannabis\n other drugs",
  "alcohol\n cannabis\n other drugs"
), full_name = c(
  "TAU", "MI", "TAU", "MI (parent)", "TAU", "MI",
  "TAU", "MI", "MI"
), article_arm_name = c(
  "Control", "WISEteens",
  "Treatment as usual", "Brief MI (b-MI)", "Assessed control",
  "Intervention", "Control", "Computer BI", "Therapist BI"
)), row.names = c(
  NA,
  -9L
), class = c("tbl_df", "tbl", "data.frame"))


ft_1 <- flextable(data_ex)
ft_1 <- theme_box(ft_1)
ft_2 <- merge_v(ft_1, j = "srdr_id",
  target = c("srdr_id", "substances"))
  ft_2
#> a flextable object.
#> col_keys: `srdr_id`, `substances`, `full_name`, `article_arm_name` 
#> header has 1 row(s) 
#> body has 9 row(s) 
#> original dataset sample: 
#>   srdr_id substances   full_name   article_arm_name
#> 1  175124    alcohol         TAU            Control
#> 2  175124    alcohol          MI          WISEteens
#> 3  172525    alcohol         TAU Treatment as usual
#> 4  172525    alcohol MI (parent)    Brief MI (b-MI)
#> 5  172545   cannabis         TAU   Assessed control