1
votes

I would like to report descriptive values in a table (I am sure they should be in a table and not in a figure). The data comes from a 3-factorial experiment, so the table that I am able to produce with xtable (I'm doing it in an Rmarkdown and Knitr and have never used LaTex) contains one line per data value along the format:

group | condition | type | value

When all the lines are printed below each other, this in not very readable, for example the "group" entry remains the same for 10 lines. Is there a possibility to just print it the first time (in the first line) and then omit it until the "group" changes to the next group (only print it in line 11)? My table should have apa-format, so I use either rapa::apa(mytable) or papaja::apa_table(mytable) for the final print. Any help would be appreciated, thanks!

3

3 Answers

2
votes

There are a few different ways to do this.

library(data.table)

dt = data.table("Group" = c(rep("A",4),rep("B",4)), "value" = rep(1:4, each = 2))
knitr::kable(dt)

> dt
   Group value
1:     A     1
2:     A     1
3:     A     2
4:     A     2
5:     B     3
6:     B     3
7:     B     4
8:     B     4

We can remove duplicates across all rows

knitr::kable(dt[!duplicated(dt),])

|Group | value|
|:-----|-----:|
|A     |     1|
|A     |     2|
|B     |     3|
|B     |     4|

Or, we can remove duplicates according to specific rows

knitr::kable(unique(dt,by = c("Group")))
|Group | value|
|:-----|-----:|
|A     |     1|
|B     |     3|

Then, since that can match to multiple options we can specify which one we want to grab

knitr::kable(dt[unique(dt,by = c("Group")),.(Group, value), mult = "first"])
|Group | value|
|:-----|-----:|
|A     |     1|
|B     |     3|

knitr::kable(dt[unique(dt,by = c("Group")),.(Group, value), mult = "last"])
|Group | value|
|:-----|-----:|
|A     |     2|
|B     |     4|

EDIT

To not print values in a specific group that have been duplicated

dt$Group = ifelse(duplicated(dt$Group),"",dt$Group)
knitr::kable(dt)
|Group | value|
|:-----|-----:|
|A     |     1|
|      |     1|
|      |     2|
|      |     2|
|B     |     3|
|      |     3|
|      |     4|
|      |     4|
0
votes

You can use duplicated function with negation (!) to retain values of "group" only at transitions but be careful that is does not result in loss of information from other columns (if they are important). In the demo datset we retain only transitions of cyl variable.

mtcarsSubset  = mtcars[,1:5]

knitr::kable(mtcarsSubset)

#|                    |  mpg| cyl|  disp|  hp| drat|
#|:-------------------|----:|---:|-----:|---:|----:|
#|Mazda RX4           | 21.0|   6| 160.0| 110| 3.90|
#|Mazda RX4 Wag       | 21.0|   6| 160.0| 110| 3.90|
#|Datsun 710          | 22.8|   4| 108.0|  93| 3.85|
#|Hornet 4 Drive      | 21.4|   6| 258.0| 110| 3.08|
#|Hornet Sportabout   | 18.7|   8| 360.0| 175| 3.15|
#|Valiant             | 18.1|   6| 225.0| 105| 2.76|
#|Duster 360          | 14.3|   8| 360.0| 245| 3.21|
#|Merc 240D           | 24.4|   4| 146.7|  62| 3.69|
#|Merc 230            | 22.8|   4| 140.8|  95| 3.92|
#|Merc 280            | 19.2|   6| 167.6| 123| 3.92|
#|Merc 280C           | 17.8|   6| 167.6| 123| 3.92|
#|Merc 450SE          | 16.4|   8| 275.8| 180| 3.07|
#|Merc 450SL          | 17.3|   8| 275.8| 180| 3.07|
#|Merc 450SLC         | 15.2|   8| 275.8| 180| 3.07|
#|Cadillac Fleetwood  | 10.4|   8| 472.0| 205| 2.93|
#|Lincoln Continental | 10.4|   8| 460.0| 215| 3.00|
#|Chrysler Imperial   | 14.7|   8| 440.0| 230| 3.23|
#|Fiat 128            | 32.4|   4|  78.7|  66| 4.08|
#|Honda Civic         | 30.4|   4|  75.7|  52| 4.93|
#|Toyota Corolla      | 33.9|   4|  71.1|  65| 4.22|
#|Toyota Corona       | 21.5|   4| 120.1|  97| 3.70|
#|Dodge Challenger    | 15.5|   8| 318.0| 150| 2.76|
#|AMC Javelin         | 15.2|   8| 304.0| 150| 3.15|
#|Camaro Z28          | 13.3|   8| 350.0| 245| 3.73|
#|Pontiac Firebird    | 19.2|   8| 400.0| 175| 3.08|
#|Fiat X1-9           | 27.3|   4|  79.0|  66| 4.08|
#|Porsche 914-2       | 26.0|   4| 120.3|  91| 4.43|
#|Lotus Europa        | 30.4|   4|  95.1| 113| 3.77|
#|Ford Pantera L      | 15.8|   8| 351.0| 264| 4.22|
#|Ferrari Dino        | 19.7|   6| 145.0| 175| 3.62|
#|Maserati Bora       | 15.0|   8| 301.0| 335| 3.54|
#|Volvo 142E          | 21.4|   4| 121.0| 109| 4.11|


knitr::kable(mtcarsSubset[!duplicated(mtcarsSubset$cyl),])

#|                  |  mpg| cyl| disp|  hp| drat|
#|:-----------------|----:|---:|----:|---:|----:|
#|Mazda RX4         | 21.0|   6|  160| 110| 3.90|
#|Datsun 710        | 22.8|   4|  108|  93| 3.85|
#|Hornet Sportabout | 18.7|   8|  360| 175| 3.15|
0
votes

Finally, I changed the data-frame that then is converted into a table. ReplicationTable %>% mutate(dependent_variable = ifelse(duplicated(dependent_variable), "", dependent_variable) This replaces all entries with an empty string after the first unique entry in dependent_variable. This also works in grouped data-frames.