Search code examples
rdplyrflextable

How to flip flop with dplyr rows and columns positions


This output of flextable function obtained with as_grouped_data() function.

df = structure(list(variable = c("something", NA, NA, NA), var = c(NA, "(Intercept)", "mutate1", "variable"), estimate = c(NA, 3.64770410229416, -0.230158472032055, -0.000692974348090823),  std.error = c(NA, 0.88, 0.0831, 0.9315), statistic = c(NA, 0.1933, -0.5458, -0.613), df = c(NA, 67.03, 53.27, 58.285), p.value = c(NA, "<0.001", "0.80", "0.87")), row.names = c(NA, 4L), class = c("grouped_data", "data.frame"))

which gives something like:

   variable         var      estimate std.error statistic     df p.value
1 something        <NA>            NA        NA        NA     NA    <NA>
2      <NA> (Intercept)  3.6477041023    0.8800    0.1933 67.030  <0.001
3      <NA>     mutate1 -0.2301584720    0.0831   -0.5458 53.270    0.80
4      <NA>    variable -0.0006929743    0.9315   -0.6130 58.285    0.87

Since I am working with flextable what that it is possible to get is:

df %>% flextable %>% colformat_double(j = c('estimate','std.error', 'statistic','df'))

enter image description here

I know that it can be considered as a piece of dataframe and I would like to do before converting it as a table, are modifications so as I could get finally with this results, possibly with dplyr

       variable        
       something   NA            NA         NA        NA  NA
       var          estimate      std.error  statistic df  p.value

        (Intercept)  3.6477041023    0.8800    0.1933 67.030  <0.001
        mutate1    -0.2301584720    0.0831   -0.5458 53.270    0.80
        variable   -0.0006929743    0.9315   -0.6130 58.285    0.87

Thus the final table that I would like to get is

     variable      NA.       NA        NA      NA      NA
1   something     <NA>      <NA>      <NA>    <NA>    <NA>
2         var estimate std.error statistic      df p.value
3 (Intercept)      3.6       0.9       0.2      67  <0.001
4      mutate     -0.2       0.1      -0.5    53.3     0.8
5    variable     -0.2       0.9       0.6    53.3    0.87

which as flextable show return

enter image description here

That just for the sake of getting the idea contains columns with X dotted and numbered NA, while those column should be in blank. Is that possible to adjust the dataframe with dplyr as I would wish.


Solution

  • Also it does not answer the dplyr question, I think it can help you to do what you want. It answers the need to add rows above a model summary in a flextable:

    library(flextable)
    options(show.signif.stars = FALSE)
    dat <- attitude
    dat$high.rating <- (dat$rating > 70)
    probit.model <- glm(high.rating ~ learning + critical +
                          advance, data = dat, family = binomial(link = "probit"))
    
    ft <- as_flextable(probit.model) |> 
      delete_part(part = "footer") |> 
      add_header_lines(values = c("variable", "something")) |> 
      theme_booktabs()
    
    ft
    

    enter image description here

    A more complete demonstration that shows how to add rows in header or in body:

    ft <- as_flextable(probit.model) |> 
      delete_part(part = "footer") |> 
      add_header_row(values = c("on 2 columns", "on 3 columns"), colwidths = c(2, 3)) |> 
      add_header_lines(values = c("variable", "something")) |> 
      add_body_row(values = c("blah blah", "bleeh"), colwidths = c(3, 2), top = TRUE) |> 
      add_body_row(values = c("bleeh bleeh", "blah"), colwidths = c(3, 2), top = FALSE) |> 
      theme_vanilla() |> 
      color(i = ~ p.value > 0.05, color = "gray") |> 
      align(part = "header", i = 1:3, align = "left")
      
    ft
    

    enter image description here