Carl H
Carl H

Reputation: 1036

Transforming coefficient table in R for knitr::kable() display

In R environment, I have a tibble() of parameter estimates from a set of models:

              Model       term           estimate
              <chr>      <chr>              <chr>
1  Equation all_rev        ECT -0.175153366498138
2  Equation all_rev  Intercept -0.813722280439735
3  Equation all_rev all_rev -1 -0.552033103080694
4  Equation all_rev    mood -1  0.149186002525841
5  Equation all_rev zsc_med -1   9.67754263298424
6  Equation all_rev zmq_med -1    4.8674015065453
7  Equation all_rev all_rev -2 -0.286127265624132
8  Equation all_rev    mood -2  0.278054170570161
9  Equation all_rev zsc_med -2  -2.36086942618573
10 Equation all_rev zmq_med -2  -2.22689475852024
   ...              ...     ... ... 

What would be an efficient way to transform the table into the formate similar to a contingency table? Such as:

              Model       term           estimate
-------------------------------------------------
1  Equation all_rev        ECT -0.175153366498138
                     Intercept -0.813722280439735
                    all_rev -1 -0.552033103080694
                       mood -1  0.149186002525841
                    zsc_med -1   9.67754263298424
                    zmq_med -1    4.8674015065453
                    all_rev -2 -0.286127265624132
                       mood -2  0.278054170570161
                    zsc_med -2  -2.36086942618573
                    zmq_med -2  -2.22689475852024
2  Equation mood           ECT -0.175153366498138
                     Intercept -0.813722280439735
                    all_rev -1 -0.552033103080694
                       mood -1  0.149186002525841
                    zsc_med -1   9.67754263298424
                    zmq_med -1    4.8674015065453
                    all_rev -2 -0.286127265624132
                       mood -2  0.278054170570161
                    zsc_med -2  -2.36086942618573
                    zmq_med -2  -2.22689475852024
...                 ...         ...

Basically, if there is a repetition in row, I would like to only have the first row value displayed. Since this is not really a contingency table and the main cells are estimates, not frequency, it is not possible to use the table() command or ftable().

In particular, I would like to use knitr::kable() to knit the result into a table that can be displayed in an PDF document.

Upvotes: 1

Views: 341

Answers (1)

eipi10
eipi10

Reputation: 93821

You can replace duplicated values with an empty string. For example:

# Fake data
set.seed(2)
dat = data.frame(Model=rep(c("Equation all_rev", "Equation mood"), each=6),
                 Term=rep(LETTERS[1:6], 2),
                 Estimate=rnorm(12), 
                 stringsAsFactors=FALSE)
library(knitr)
library(dplyr)

kable(dat %>% mutate(Model = ifelse(duplicated(Model), "", as.character(Model))))
|Model            |Term |   Estimate|
|:----------------|:----|----------:|
|Equation all_rev |A    | -0.8969145|
|                 |B    |  0.1848492|
|                 |C    |  1.5878453|
|                 |D    | -1.1303757|
|                 |E    | -0.0802518|
|                 |F    |  0.1324203|
|Equation mood    |A    |  0.7079547|
|                 |B    | -0.2396980|
|                 |C    |  1.9844739|
|                 |D    | -0.1387870|
|                 |E    |  0.4176508|
|                 |F    |  0.9817528|

Sometimes, you might have several hierarchical columns where you need to remove repeated values within subgroups. duplicated alone doesn't work in this case. There are a few ways to proceed. Here's one example:

# Add a second column with repeating sub-sets of the Model column
dat = dat %>% mutate(Sub_Model=rep(rep(c("Mal","Serenity"), each=3),2)) %>%
  select(1,4,2,3)

# Remove repeated values by group
dat[1:nrow(dat) %% 6 != 1, 1] = ""
dat[1:nrow(dat) %% 3 != 1,2] = ""

kable(dat)
|Model            |Sub_Model |   Estimate|Term |
|:----------------|:---------|----------:|:----|
|Equation all_rev |Mal       | -0.8969145|A    |
|                 |          |  0.1848492|B    |
|                 |          |  1.5878453|C    |
|                 |Serenity  | -1.1303757|D    |
|                 |          | -0.0802518|E    |
|                 |          |  0.1324203|F    |
|Equation mood    |Mal       |  0.7079547|A    |
|                 |          | -0.2396980|B    |
|                 |          |  1.9844739|C    |
|                 |Serenity  | -0.1387870|D    |
|                 |          |  0.4176508|E    |
|                 |          |  0.9817528|F    |

If you use xtable instead of kable, you also add additional formatting, such as horizontal lines or partial horizontal lines.

Upvotes: 2

Related Questions