0

In the example below I would like to vertically merge like values in column 1 and then vertically merge like values in column 2 conditional on them being in the same group in column 1. Right now it currently merges "G2" across groups "a" and "b" from v1, which is not what I'd like. Is there a way to achieve this using a built in flextable function or like wise?

library(flextable)

dat <- data.frame(v1 = c(rep("a", 3), rep("b", 3), rep("c", 3)),
                  v2 = c("G1", "G1", "G2", "G2", "G3", "G3", "G4", "G5", "G6"),
                  v3 = c(1:9), stringsAsFactors = FALSE)

ft <- regulartable(dat)
ft <- merge_v(ft,j=c(1:2))

Expected output

Jordan Hackett
  • 689
  • 3
  • 11

1 Answers1

1

I would create a variable for that (and use it but not display it):

library(flextable)

dat <- data.frame(v1 = c(rep("a", 3), rep("b", 3), rep("c", 3)),
                  v2 = c("G1", "G1", "G2", "G2", "G3", "G3", "G4", "G5", "G6"),
                  v3 = c(1:9), 
                  stringsAsFactors = FALSE)
dat$v_dummy <- paste0(dat$v1, dat$v2)
ft <- flextable(dat, col_keys = c("v1", "v2", "v3"))
ft <- theme_box(ft)
ft <- merge_v(ft, j = c("v1", "v_dummy"), target = c("v1", "v2"))
ft

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Sorry David I may have not explained well enough, that is not exactly what I'm looking for. What I would like in column "v2" is for only cells (1,2) and (5,6) to be merged together. I.e. NOT cells (3,4) because the first "G2" belongs to group "a" and the second "G2" belongs to group "b". Does that make sense? – Jordan Hackett Jun 23 '20 at 16:06
  • Could you post an img/illustration of what you are expecting? – David Gohel Jun 25 '20 at 18:05
  • I've edited the original post with a picture of anticipated output. Notice how v2 is only merged when contained within the same group v1. – Jordan Hackett Jun 25 '20 at 18:52
  • Thanks so much for your time David. This is exactly what I was looking for :)! – Jordan Hackett Jun 26 '20 at 18:05