Skip to contents

Converts a crosstable object into a formatted gt table.

Method to convert an object to a gt table

Default method to convert an object to a gt table

Usage

# S3 method for class 'crosstable'
as_gt(
  x,
  show_test_name = TRUE,
  by_header = NULL,
  keep_id = FALSE,
  generic_labels = list(id = ".id", variable = "variable", value = "value", total =
    "Total", label = "label", test = "test", effect = "effect"),
  ...
)

as_gt(x, ...)

# Default S3 method
as_gt(x, ...)

Arguments

x

object to be converted

show_test_name

in the test column, show the test name

by_header

a string to override the by header

keep_id

whether to keep the .id column

generic_labels

names of the crosstable default columns

...

arguments for custom methods

Value

a formatted gt table

Methods (by class)

  • as_gt(crosstable): For crosstables

  • as_gt(default): default function

Author

Dan Chaltiel

Examples

xx = mtcars2 %>% dplyr::select(2:10)
crosstable(xx) %>% as_gt
value
Miles/(US) gallon
Min / Max 10.4 / 33.9
Med [IQR] 19.2 [15.4;22.8]
Mean (std) 20.1 (6.0)
N (NA) 32 (0)
Number of cylinders
4 11 (34.38%)
6 7 (21.88%)
8 14 (43.75%)
Displacement (cu.in.)
Min / Max 71.1 / 472.0
Med [IQR] 196.3 [120.8;326.0]
Mean (std) 230.7 (123.9)
N (NA) 32 (0)
Gross horsepower
Min / Max 52.0 / 335.0
Med [IQR] 123.0 [96.5;180.0]
Mean (std) 146.7 (68.6)
N (NA) 32 (0)
Rear axle ratio
Min / Max 2.8 / 4.9
Med [IQR] 3.7 [3.1;3.9]
Mean (std) 3.6 (0.5)
N (NA) 32 (0)
Weight (1000 lbs)
Min / Max 1.5 / 5.4
Med [IQR] 3.3 [2.6;3.6]
Mean (std) 3.2 (1.0)
N (NA) 32 (0)
1/4 mile time
Min / Max 14.5 / 22.9
Med [IQR] 17.7 [16.9;18.9]
Mean (std) 17.8 (1.8)
N (NA) 32 (0)
Engine
straight 14 (43.75%)
vshaped 18 (56.25%)
Transmission
auto 19 (59.38%)
manual 13 (40.62%)
crosstable(xx, by=am) %>% as_gt #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead
Transmission
auto manual
Miles/(US) gallon
Min / Max 10.4 / 24.4 15.0 / 33.9
Med [IQR] 17.3 [14.9;19.2] 22.8 [21.0;30.4]
Mean (std) 17.1 (3.8) 24.4 (6.2)
N (NA) 19 (0) 13 (0)
Number of cylinders
4 3 (27.27%) 8 (72.73%)
6 4 (57.14%) 3 (42.86%)
8 12 (85.71%) 2 (14.29%)
Displacement (cu.in.)
Min / Max 120.1 / 472.0 71.1 / 351.0
Med [IQR] 275.8 [196.3;360.0] 120.3 [79.0;160.0]
Mean (std) 290.4 (110.2) 143.5 (87.2)
N (NA) 19 (0) 13 (0)
Gross horsepower
Min / Max 62.0 / 245.0 52.0 / 335.0
Med [IQR] 175.0 [116.5;192.5] 109.0 [66.0;113.0]
Mean (std) 160.3 (53.9) 126.8 (84.1)
N (NA) 19 (0) 13 (0)
Rear axle ratio
Min / Max 2.8 / 3.9 3.5 / 4.9
Med [IQR] 3.1 [3.1;3.7] 4.1 [3.9;4.2]
Mean (std) 3.3 (0.4) 4.0 (0.4)
N (NA) 19 (0) 13 (0)
Weight (1000 lbs)
Min / Max 2.5 / 5.4 1.5 / 3.6
Med [IQR] 3.5 [3.4;3.8] 2.3 [1.9;2.8]
Mean (std) 3.8 (0.8) 2.4 (0.6)
N (NA) 19 (0) 13 (0)
1/4 mile time
Min / Max 15.4 / 22.9 14.5 / 19.9
Med [IQR] 17.8 [17.2;19.2] 17.0 [16.5;18.6]
Mean (std) 18.2 (1.8) 17.4 (1.8)
N (NA) 19 (0) 13 (0)
Engine
straight 7 (50.00%) 7 (50.00%)
vshaped 12 (66.67%) 6 (33.33%)
crosstable(xx, by=cyl, test=TRUE, total=TRUE) %>% as_gt(keep_id=TRUE, show_test_name=FALSE, by_header="Cylinders") #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead #> Warning: Direct call of 'as.data.frame.numeric()' is deprecated. Use 'as.data.frame.vector()' or 'as.data.frame()' instead
Cylinders
Total test
4 6 8
Miles/(US) gallon (`mpg`)
Min / Max 21.4 / 33.9 17.8 / 21.4 10.4 / 19.2 10.4 / 33.9 p value: <0.0001
Med [IQR] 26.0 [22.8;30.4] 19.7 [18.6;21.0] 15.2 [14.4;16.2] 19.2 [15.4;22.8]
Mean (std) 26.7 (4.5) 19.7 (1.5) 15.1 (2.6) 20.1 (6.0)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
Displacement (cu.in.) (`disp`)
Min / Max 71.1 / 146.7 145.0 / 258.0 275.8 / 472.0 71.1 / 472.0
Med [IQR] 108.0 [78.8;120.7] 167.6 [160.0;196.3] 350.5 [301.8;390.0] 196.3 [120.8;326.0]
Mean (std) 105.1 (26.9) 183.3 (41.6) 353.1 (67.8) 230.7 (123.9)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
Gross horsepower (`hp`)
Min / Max 52.0 / 113.0 105.0 / 175.0 150.0 / 335.0 52.0 / 335.0
Med [IQR] 91.0 [65.5;96.0] 110.0 [110.0;123.0] 192.5 [176.2;241.2] 123.0 [96.5;180.0]
Mean (std) 82.6 (20.9) 122.3 (24.3) 209.2 (51.0) 146.7 (68.6)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
Rear axle ratio (`drat`)
Min / Max 3.7 / 4.9 2.8 / 3.9 2.8 / 4.2 2.8 / 4.9 p value: 0.0007
Med [IQR] 4.1 [3.8;4.2] 3.9 [3.4;3.9] 3.1 [3.1;3.2] 3.7 [3.1;3.9]
Mean (std) 4.1 (0.4) 3.6 (0.5) 3.2 (0.4) 3.6 (0.5)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
Weight (1000 lbs) (`wt`)
Min / Max 1.5 / 3.2 2.6 / 3.5 3.2 / 5.4 1.5 / 5.4 p value: <0.0001
Med [IQR] 2.2 [1.9;2.6] 3.2 [2.8;3.4] 3.8 [3.5;4.0] 3.3 [2.6;3.6]
Mean (std) 2.3 (0.6) 3.1 (0.4) 4.0 (0.8) 3.2 (1.0)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
1/4 mile time (`qsec`)
Min / Max 16.7 / 22.9 15.5 / 20.2 14.5 / 18.0 14.5 / 22.9 p value: 0.0062
Med [IQR] 18.9 [18.6;19.9] 18.3 [16.7;19.2] 17.2 [16.1;17.6] 17.7 [16.9;18.9]
Mean (std) 19.1 (1.7) 18.0 (1.7) 16.8 (1.2) 17.8 (1.8)
N (NA) 11 (0) 7 (0) 14 (0) 32 (0)
Engine (`vs`)
straight 10 (71.43%) 4 (28.57%) 0 (0%) 14 (43.75%) p value: <0.0001
vshaped 1 (5.56%) 3 (16.67%) 14 (77.78%) 18 (56.25%)
Total 11 (34.38%) 7 (21.88%) 14 (43.75%) 32 (100.00%)
Transmission (`am`)
auto 3 (15.79%) 4 (21.05%) 12 (63.16%) 19 (59.38%) p value: 0.0091
manual 8 (61.54%) 3 (23.08%) 2 (15.38%) 13 (40.62%)
Total 11 (34.38%) 7 (21.88%) 14 (43.75%) 32 (100.00%)