Having problems making a contingency table... a column should be listed as row but how?

Hi

Here's my data:

data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
...1 = c("Correct", "Incorrect", "Nomatch", "Share"),
99p = c(106, 111, 43, 126),
98p = c(80, 116, 37, 153),
97p = c(74, 122, 22, 168)
)

I want to create a contingency table, but when I write:

dt <- as.table(as.matrix(data))

I get a weird looking table:
dt
...1 99p 98p 97p
A Correct 106 80 74
B Incorrect 111 116 122
C Nomatch 43 37 22
D Share 126 153 168

I don't want those letters there (A,B,C,D). I only want the categories alone.

Thank you!!

Hi,

If I understand you correctly, you want to do this:

myData = data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  x = c("Correct", "Incorrect", "Nomatch", "Share"),
  p99 = c(106, 111, 43, 126),
  p98 = c(80, 116, 37, 153),
  p97 = c(74, 122, 22, 168)
)

myTable = as.table(as.matrix(myData[,-1]))
rownames(myTable) = myData$x

myTable
#>           p99 p98 p97
#> Correct   106  80  74
#> Incorrect 111 116 122
#> Nomatch    43  37  22
#> Share     126 153 168

Created on 2022-02-25 by the reprex package (v2.0.1)

So in this case the categories are the rownames of the tables and the table itself contains the values.

Hope this helps,
PJ

1 Like

Perfect, thank you!!

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.