Contingency table freq (percent

Hi R Studio Community,
I would like to provide a contingency table {freq (percent)}. I have this code which provide percent (freq), If somebody can help with with fixing the code that provide freq (percent) or provide me a different code that provide the expected result. It is appreciated. Thanks

# data
data <- data.frame(Id = c(1L,	1L,	1L,	1L,	1L,	2L,	2L,	2L,	2L,	3L,	3L,	3L,	3L,	3L,	4L,	4L,	4L,	4L,	4L),
                   date1 = c("2020-01-20",	"2020-02-20",	"2020-03-20",	"2020-04-20",	"2020-05-10",	"2020-01-20",	"2020-02-15",	"2020-03-20",	"2020-04-15",	"2020-05-10",	"2020-07-15",	"2020-08-15",	"2020-10-25",	"2020-11-10",	"2020-04-10",	"2020-04-10",	"2020-08-15",	"2020-10-25",	"2020-10-27"),
                   date2 = c("2020-01-20",	"2020-03-25",	"2020-03-20",	"2020-05-15",	"2020-06-12",	"2020-02-20",	"2020-03-20",	"2020-04-22",	"2020-05-15",	"2020-06-12",	"2020-08-20",	"2020-09-22",	"2020-11-15",	"2021-01-12",	"2020-05-12",	"2020-08-20",	"2020-09-22",	"2020-11-15",	"2020-10-29"),
                   date3 = c("2020-02-20",	"2020-04-25",	"2020-03-29",	"2020-06-15",	"2020-06-22",	"2020-03-20",	"2020-04-20",	"2020-05-22",	"2020-06-15",	"2020-06-22",	"2020-08-30",	"2020-09-27",	"2020-11-19",	"2021-01-28",	"2020-05-27",	"2020-08-26",	"2020-09-28",	"2020-11-18",	"2020-111-27"),
                   date4 = c("2020-03-20",	"2020-05-25",	"2020-03-30",	"2020-07-15",	"2020-06-29",	"2020-03-25",	"2020-04-27",	"2020-05-28",	"2020-06-19",	"2020-06-28",	"2020-09-30",	"2020-10-27",	"2020-11-29",	"2021-01-28",	"2020-06-27",	"2020-09-26",	"2020-09-28",	"2020-12-18",	"2020-111-28"),
                   Trt1 = factor(c("A",	"B",	"C",	"A",	"D",	NA,	"B",	"C",	"D",	"A",	"B",	NA,	"C",	"D",	"D",	"B",	"C",	"C",	"D")),
                   Trt2 = factor(c("A",	"D",	"A",	NA,	"D",	NA,	"A",	NA,	"A",	"B",	"B",	NA,	"A",	"A",	"A",	"C",	"A",	"A",	"B")),
                   Trt3 = factor(c("A",	"C",	"B",	"B",	"D",	NA,	"C",	"A",	"B",	"D",	"B",	NA,	"C",	"B",	"D",	"B",	"B",	"C",	"D")),
                   Trt4 = factor(c("C",	"A",	"B",	"A",	"D",	NA,	NA,	"C",	"A",	NA,	"B",	NA,	"A",	"D",	"A",	"C",	"C",	"A",	"B")),
                   Level = factor(c("High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High")),
                   stringsAsFactors = FALSE)

# contingency table

library("janitor")
data %>%
  tabyl(Level,Trt4,show_missing_levels = FALSE)%>%
  adorn_totals(c("row", "col")) %>%
  adorn_percentages("col") %>% 
  adorn_pct_formatting(rounding = "half up", digits = 1) %>%
  adorn_ns() %>%
  adorn_title("combined") %>%
  knitr::kable()
d <- data.frame(
  Id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L),
  date1 = c("2020-01-20", "2020-02-20", "2020-03-20", "2020-04-20", "2020-05-10", "2020-01-20", "2020-02-15", "2020-03-20", "2020-04-15", "2020-05-10", "2020-07-15", "2020-08-15", "2020-10-25", "2020-11-10", "2020-04-10", "2020-04-10", "2020-08-15", "2020-10-25", "2020-10-27"),
  date2 = c("2020-01-20", "2020-03-25", "2020-03-20", "2020-05-15", "2020-06-12", "2020-02-20", "2020-03-20", "2020-04-22", "2020-05-15", "2020-06-12", "2020-08-20", "2020-09-22", "2020-11-15", "2021-01-12", "2020-05-12", "2020-08-20", "2020-09-22", "2020-11-15", "2020-10-29"),
  date3 = c("2020-02-20", "2020-04-25", "2020-03-29", "2020-06-15", "2020-06-22", "2020-03-20", "2020-04-20", "2020-05-22", "2020-06-15", "2020-06-22", "2020-08-30", "2020-09-27", "2020-11-19", "2021-01-28", "2020-05-27", "2020-08-26", "2020-09-28", "2020-11-18", "2020-111-27"),
  date4 = c("2020-03-20", "2020-05-25", "2020-03-30", "2020-07-15", "2020-06-29", "2020-03-25", "2020-04-27", "2020-05-28", "2020-06-19", "2020-06-28", "2020-09-30", "2020-10-27", "2020-11-29", "2021-01-28", "2020-06-27", "2020-09-26", "2020-09-28", "2020-12-18", "2020-111-28"),
  Trt1 = factor(c("A", "B", "C", "A", "D", NA, "B", "C", "D", "A", "B", NA, "C", "D", "D", "B", "C", "C", "D")),
  Trt2 = factor(c("A", "D", "A", NA, "D", NA, "A", NA, "A", "B", "B", NA, "A", "A", "A", "C", "A", "A", "B")),
  Trt3 = factor(c("A", "C", "B", "B", "D", NA, "C", "A", "B", "D", "B", NA, "C", "B", "D", "B", "B", "C", "D")),
  Trt4 = factor(c("C", "A", "B", "A", "D", NA, NA, "C", "A", NA, "B", NA, "A", "D", "A", "C", "C", "A", "B")),
  Level = factor(c("High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High")),
  stringsAsFactors = FALSE
)

d |>
  tabyl(Level,Trt4,show_missing_levels = FALSE) |>
  adorn_totals(c("row", "col")) |>
  adorn_percentages("col") |> 
  adorn_pct_formatting(rounding = "half up", digits = 1) |>
  adorn_ns() |>
  adorn_title("combined") |>
  knitr::kable()
#> Error in adorn_title(adorn_ns(adorn_pct_formatting(adorn_percentages(adorn_totals(tabyl(d, : could not find function "adorn_title"




(t1 <- table(d$Trt1,d$Level) |> proportions())
#>    
#>           High        Low     Medium
#>   A 0.17647059 0.00000000 0.00000000
#>   B 0.11764706 0.00000000 0.11764706
#>   C 0.05882353 0.11764706 0.11764706
#>   D 0.05882353 0.11764706 0.11764706

(t2 <- t1 * 100)
#>    
#>          High       Low    Medium
#>   A 17.647059  0.000000  0.000000
#>   B 11.764706  0.000000 11.764706
#>   C  5.882353 11.764706 11.764706
#>   D  5.882353 11.764706 11.764706

(t3 <- round(t2,2))
#>    
#>      High   Low Medium
#>   A 17.65  0.00   0.00
#>   B 11.76  0.00  11.76
#>   C  5.88 11.76  11.76
#>   D  5.88 11.76  11.76

Created on 2023-02-21 with reprex v2.0.2

Thank you very much for the response and support. I would like a table that shows frequency (percent). I have this percent (freq), but need help to modify it. Thanks

# data
data <- data.frame(Id = c(1L,	1L,	1L,	1L,	1L,	2L,	2L,	2L,	2L,	3L,	3L,	3L,	3L,	3L,	4L,	4L,	4L,	4L,	4L),
                   date1 = c("2020-01-20",	"2020-02-20",	"2020-03-20",	"2020-04-20",	"2020-05-10",	"2020-01-20",	"2020-02-15",	"2020-03-20",	"2020-04-15",	"2020-05-10",	"2020-07-15",	"2020-08-15",	"2020-10-25",	"2020-11-10",	"2020-04-10",	"2020-04-10",	"2020-08-15",	"2020-10-25",	"2020-10-27"),
                   date2 = c("2020-01-20",	"2020-03-25",	"2020-03-20",	"2020-05-15",	"2020-06-12",	"2020-02-20",	"2020-03-20",	"2020-04-22",	"2020-05-15",	"2020-06-12",	"2020-08-20",	"2020-09-22",	"2020-11-15",	"2021-01-12",	"2020-05-12",	"2020-08-20",	"2020-09-22",	"2020-11-15",	"2020-10-29"),
                   date3 = c("2020-02-20",	"2020-04-25",	"2020-03-29",	"2020-06-15",	"2020-06-22",	"2020-03-20",	"2020-04-20",	"2020-05-22",	"2020-06-15",	"2020-06-22",	"2020-08-30",	"2020-09-27",	"2020-11-19",	"2021-01-28",	"2020-05-27",	"2020-08-26",	"2020-09-28",	"2020-11-18",	"2020-111-27"),
                   date4 = c("2020-03-20",	"2020-05-25",	"2020-03-30",	"2020-07-15",	"2020-06-29",	"2020-03-25",	"2020-04-27",	"2020-05-28",	"2020-06-19",	"2020-06-28",	"2020-09-30",	"2020-10-27",	"2020-11-29",	"2021-01-28",	"2020-06-27",	"2020-09-26",	"2020-09-28",	"2020-12-18",	"2020-111-28"),
                   Trt1 = factor(c("A",	"B",	"C",	"A",	"D",	NA,	"B",	"C",	"D",	"A",	"B",	NA,	"C",	"D",	"D",	"B",	"C",	"C",	"D")),
                   Trt2 = factor(c("A",	"D",	"A",	NA,	"D",	NA,	"A",	NA,	"A",	"B",	"B",	NA,	"A",	"A",	"A",	"C",	"A",	"A",	"B")),
                   Trt3 = factor(c("A",	"C",	"B",	"B",	"D",	NA,	"C",	"A",	"B",	"D",	"B",	NA,	"C",	"B",	"D",	"B",	"B",	"C",	"D")),
                   Trt4 = factor(c("C",	"A",	"B",	"A",	"D",	NA,	NA,	"C",	"A",	NA,	"B",	NA,	"A",	"D",	"A",	"C",	"C",	"A",	"B")),
                   Level = factor(c("High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High",	"Medium",	"Low",	"High")),
                   stringsAsFactors = FALSE)

# contingency table

library("janitor")
data %>%
  tabyl(Level,Trt4,show_missing_levels = FALSE)%>%
  adorn_totals(c("row", "col")) %>%
  adorn_percentages("col") %>% 
  adorn_pct_formatting(rounding = "half up", digits = 1) %>%
  adorn_ns() %>%
  adorn_title("combined") %>%
  knitr::kable()

I misunderstood, but now I'm unclear how the output shown needs modification.

Hi, I need a table that shows frequency and percent together. The code I use, provide percent (freq) as above, I need a table that shows freq (percent). Hope it is clear now. Thanks

Just to check, then,

33.3% (2) should be 2 (33.3%), is it?

That is correct. If possible you can modify this table or I need one to do the same job 2 (33.3 %) or 2 (33.3) or 2 (.333). Thanks

If the output is intercepted before it reaches knitr::kable(), it's possible to reverse the order of percentage, frequency

library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
d <- data.frame(
  Id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L),
  date1 = c("2020-01-20", "2020-02-20", "2020-03-20", "2020-04-20", "2020-05-10", "2020-01-20", "2020-02-15", "2020-03-20", "2020-04-15", "2020-05-10", "2020-07-15", "2020-08-15", "2020-10-25", "2020-11-10", "2020-04-10", "2020-04-10", "2020-08-15", "2020-10-25", "2020-10-27"),
  date2 = c("2020-01-20", "2020-03-25", "2020-03-20", "2020-05-15", "2020-06-12", "2020-02-20", "2020-03-20", "2020-04-22", "2020-05-15", "2020-06-12", "2020-08-20", "2020-09-22", "2020-11-15", "2021-01-12", "2020-05-12", "2020-08-20", "2020-09-22", "2020-11-15", "2020-10-29"),
  date3 = c("2020-02-20", "2020-04-25", "2020-03-29", "2020-06-15", "2020-06-22", "2020-03-20", "2020-04-20", "2020-05-22", "2020-06-15", "2020-06-22", "2020-08-30", "2020-09-27", "2020-11-19", "2021-01-28", "2020-05-27", "2020-08-26", "2020-09-28", "2020-11-18", "2020-111-27"),
  date4 = c("2020-03-20", "2020-05-25", "2020-03-30", "2020-07-15", "2020-06-29", "2020-03-25", "2020-04-27", "2020-05-28", "2020-06-19", "2020-06-28", "2020-09-30", "2020-10-27", "2020-11-29", "2021-01-28", "2020-06-27", "2020-09-26", "2020-09-28", "2020-12-18", "2020-111-28"),
  Trt1 = factor(c("A", "B", "C", "A", "D", NA, "B", "C", "D", "A", "B", NA, "C", "D", "D", "B", "C", "C", "D")),
  Trt2 = factor(c("A", "D", "A", NA, "D", NA, "A", NA, "A", "B", "B", NA, "A", "A", "A", "C", "A", "A", "B")),
  Trt3 = factor(c("A", "C", "B", "B", "D", NA, "C", "A", "B", "D", "B", NA, "C", "B", "D", "B", "B", "C", "D")),
  Trt4 = factor(c("C", "A", "B", "A", "D", NA, NA, "C", "A", NA, "B", NA, "A", "D", "A", "C", "C", "A", "B")),
  Level = factor(c("High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High", "Medium", "Low", "High")),
  stringsAsFactors = FALSE
)

o <- d |>
  tabyl(Level,Trt4,show_missing_levels = FALSE) |>
  adorn_totals(c("row", "col")) |>
  adorn_percentages("col") |> 
  adorn_pct_formatting(rounding = "half up", digits = 1) |>
  adorn_ns() |>
  adorn_title("combined") 

x <- o[2,][2][[1]]
strsplit(x," ")[[1]][2:1]
#> [1] "(3)"   "50.0%"

However, I haven't been able to do that except on an entry-by-entry basis. At this point, if the order were a preference, I'd change it and if it were a requirement, I'd drop tabyl() and work directly with the input to produce the required layout.

Hi, I would like to do it for whole table or using other functions are also ok as far as they provide the expected result 3 (50%). Thanks

The whole table is what was not able to do. Maybe the taybl source code can be tweaked. I’ll check.