How to make such a frequency/contingency table in R

Hi All,

I would like to do frequency/contingency table like that, I have created in pivot tables in Excel:

Here is my dataframe:

data <- structure(list(
  Lp = 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"
  ), Group = structure(c(
    1L, 1L, 2L, 2L, 1L, 1L, 2L,
    2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
    2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
  ), levels = c(
    "Radiowaves",
    "Soniccurrents"
  ), class = "factor"), Gender = structure(c(
    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
  ), levels = c(
    "K",
    "M"
  ), class = "factor"), Time = structure(c(
    1L, 1L, 1L, 1L, 2L,
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L,
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L
  ), levels = c(
    "before_treatment",
    "two_procedures", "three_procedures", "after_treatment"
  ), class = "factor"),
  Value_measured = c(
    3, 5, 3, 7, 1, 5, 3, 5, 3, 1, 5, 7, 0,
    5, 1, 5, 2, 4, 1, 4, 2, 5, 4, 6, 2, 5, 4, 2, 2, 4, 6, 1
  )
), class = c(
  "grouped_df",
  "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -32L), groups = structure(list(
  Gender = structure(c(
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L
  ), levels = c("K", "M"), class = "factor"),
  Time = structure(c(
    1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L,
    2L, 2L, 3L, 3L, 4L, 4L
  ), levels = c(
    "before_treatment", "two_procedures",
    "three_procedures", "after_treatment"
  ), class = "factor"),
  Group = structure(c(
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
    1L, 2L, 1L, 2L, 1L, 2L
  ), levels = c("Radiowaves", "Soniccurrents"), class = "factor"), .rows = structure(list(
    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
  ), ptype = integer(0), class = c(
    "vctrs_list_of",
    "vctrs_vctr", "list"
  ))
), row.names = c(NA, -16L), class = c(
  "tbl_df",
  "tbl", "data.frame"
), .drop = TRUE))

Any help will be greatly appreciated,
thank you.

This topic was automatically closed 21 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.