Crosstab for contrasts

Hi, I try to grasp a concept of contrasts (planned comparisons) in R.
I need to create such a table:

This simple table will illustrate what could be compared with what, and how many comparisons do we have to do ?

Here is my data:

``````dat <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48), Self_control = c(65, 70, 60, 60, 60, 55, 60,
55, 70, 65, 60, 70, 65, 60, 60, 50, 55, 65, 70, 55, 55, 60, 50,
50, 50, 55, 80, 65, 70, 75, 75, 65, 45, 60, 85, 65, 70, 70, 80,
60, 30, 30, 30, 55, 35, 20, 45, 40), Sex = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Female",
"Male"), class = "factor"), Alcohol = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("None",
"2 Pints", "4 Pints"), class = "factor")), row.names = c(NA,
48L), class = "data.frame")

# Factor:
dat\$Sex <- factor(dat\$Sex, levels=c("Male", "Female"),
labels=c("Male", "Female"))

dat\$Sex <- relevel(dat\$Sex, ref="Female")

levels(dat\$Sex)
``````

Obviously when I use:

``````contrasts(dat\$Alcohol)

contr.treatment(levels(dat\$Alcohol))
``````

It gives me:

but it looks rather cryptic. So I have figured that my table will be better to understand what is going on for a starting point.

How do I create such a table ? How do I start, please ? Any help will be much appreciated.
Maybe a ready function exists in R to do it ?

kind regards,

maybe :

``````alc <- c("None", "2 Pints", "4 Pints")
alcohol <- factor(alc,levels=alc)
(a1 <- contrasts(alcohol))
a2 <- cbind(1-rowSums(a1),a1)
colnames(a2)<- rownames(a2)
a2
``````

giving

``````        None 2 Pints 4 Pints
None       1       0       0
2 Pints    0       1       0
4 Pints    0       0       1``````

What does it show us ? This is different from my desired result.

Do you further want to change 1 and 0 to mathematical symbols + and × ?

Yes, I do, if possible, or other symbols just to indicate possible comparisons.

``````alc <- c("None", "2 Pints", "4 Pints")
alcohol <- factor(alc,levels=alc)
(a1 <- contrasts(alcohol))
a2 <- cbind(1-rowSums(a1),a1)
colnames(a2)<- rownames(a2)
a2
library(tidyverse)
(a3 <- as.data.frame(a2) |> mutate_all(
\(x)case_match(
x,
0~'+',
1~'x'
)
))
``````
``````        None 2 Pints 4 Pints
None       x       +       +
2 Pints    +       x       +
4 Pints    +       +       x
``````

Is it possible somehow to add rows and columns totals summing pluses up ?

``````(a4 <- rownames_to_column(a3,var = " "))
(a5 <- bind_rows(
a4,
a4 |> summarise(across(rownames(a3),
.fns = \(x_)as.character(sum(x_ == "+"))),
across(" ", \(x_)"Total")
))
)
``````
``````          None 2 Pints 4 Pints
1    None    x       +       +
2 2 Pints    +       x       +
3 4 Pints    +       +       x
4   Total    2       2       2``````

Thank you very much indeed for your kind help. There is no rows total column but I assume that it would be difficult to achieve.
The final table looks very nice and will be useful for me.

``````(a4 <- rownames_to_column(a3,var = " "))
(a5 <- bind_rows(
a4 |>  rowwise() |> mutate("Total"= sum(c_across(-" ")=='+')),
a4 |> summarise(across(rownames(a3),
.fns = \(x_)as.character(sum(x_ == "+"))),
across(" ", \(x_)"Total")) |>
rowwise() |> mutate("Total"= sum(as.numeric(c_across(-" "))))
) |> tibble()
)
``````
``````# A tibble: 4 × 5
` `     None  `2 Pints` `4 Pints` Total
<chr>   <chr> <chr>     <chr>     <dbl>
1 None    x     +         +             2
2 2 Pints +     x         +             2
3 4 Pints +     +         x             2
4 Total   2     2         2             6``````

I would like to thank you unreservedly, be blessed, safe and healthy - always.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.