From crosstab 2x2 or 3x3 table to normal flat table

Hi,

It is not immediately clear how you want us to help. It always helps to add a reprex (see here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners ).

You will see below that I have created a base R way and melt way to take a table and convert it into a list of values. Let me know if this is what you wanted?

# load table     
crosstable <- structure(list(col1 = c(0, 0, 1, 0, 0, 0, 0, 0, 0), col2 = c(0, 1, 0, 0, 0, 0, 0, 0, 0), col3 = c(0, 0, 0, 0, 0, 0, 0, 0, 1), col4 = c(1, 0, 0, 0, 0, 0, 0, 0, 0), col5 = c(0, 0, 0, 0, 0, 0, 0, 1, 0), col6 = c(0, 0, 0, 0, 0, 1, 0, 0, 0), col7 = c(0, 0, 0, 0, 1, 0, 0, 0, 0), col8 = c(0, 0, 0, 0, 0, 0, 1, 0, 0)), .Names = c("col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8"), row.names = c("row1", "row2", "row3", "row4", "row5", "row6", "row7", "row8", "row9"), class = "data.frame")

#display table
crosstable
#>      col1 col2 col3 col4 col5 col6 col7 col8
#> row1    0    0    0    1    0    0    0    0
#> row2    0    1    0    0    0    0    0    0
#> row3    1    0    0    0    0    0    0    0
#> row4    0    0    0    0    0    0    0    0
#> row5    0    0    0    0    0    0    1    0
#> row6    0    0    0    0    0    1    0    0
#> row7    0    0    0    0    0    0    0    1
#> row8    0    0    0    0    1    0    0    0
#> row9    0    0    1    0    0    0    0    0

CTLong <- data.frame(rows = rownames(crosstable), stack(crosstable))
CTLong <- CTLong[order(CTLong$rows), ]

CTLong
#>    rows values  ind
#> 1  row1      0 col1
#> 10 row1      0 col2
#> 19 row1      0 col3
#> 28 row1      1 col4
#> 37 row1      0 col5
#> 46 row1      0 col6
#> 55 row1      0 col7
#> 64 row1      0 col8
#> 2  row2      0 col1
#> 11 row2      1 col2
#> 20 row2      0 col3
#> 29 row2      0 col4
#> 38 row2      0 col5
#> 47 row2      0 col6
#> 56 row2      0 col7
#> 65 row2      0 col8
#> 3  row3      1 col1
#> 12 row3      0 col2
#> 21 row3      0 col3
#> 30 row3      0 col4
#> 39 row3      0 col5
#> 48 row3      0 col6
#> 57 row3      0 col7
#> 66 row3      0 col8
#> 4  row4      0 col1
#> 13 row4      0 col2
#> 22 row4      0 col3
#> 31 row4      0 col4
#> 40 row4      0 col5
#> 49 row4      0 col6
#> 58 row4      0 col7
#> 67 row4      0 col8
#> 5  row5      0 col1
#> 14 row5      0 col2
#> 23 row5      0 col3
#> 32 row5      0 col4
#> 41 row5      0 col5
#> 50 row5      0 col6
#> 59 row5      1 col7
#> 68 row5      0 col8
#> 6  row6      0 col1
#> 15 row6      0 col2
#> 24 row6      0 col3
#> 33 row6      0 col4
#> 42 row6      0 col5
#> 51 row6      1 col6
#> 60 row6      0 col7
#> 69 row6      0 col8
#> 7  row7      0 col1
#> 16 row7      0 col2
#> 25 row7      0 col3
#> 34 row7      0 col4
#> 43 row7      0 col5
#> 52 row7      0 col6
#> 61 row7      0 col7
#> 70 row7      1 col8
#> 8  row8      0 col1
#> 17 row8      0 col2
#> 26 row8      0 col3
#> 35 row8      0 col4
#> 44 row8      1 col5
#> 53 row8      0 col6
#> 62 row8      0 col7
#> 71 row8      0 col8
#> 9  row9      0 col1
#> 18 row9      0 col2
#> 27 row9      1 col3
#> 36 row9      0 col4
#> 45 row9      0 col5
#> 54 row9      0 col6
#> 63 row9      0 col7
#> 72 row9      0 col8


library(reshape2)
CTLong2 <- melt(cbind(rownames(crosstable),crosstable))
#> Using rownames(crosstable) as id variables

CTLong2
#>    rownames(crosstable) variable value
#> 1                  row1     col1     0
#> 2                  row2     col1     0
#> 3                  row3     col1     1
#> 4                  row4     col1     0
#> 5                  row5     col1     0
#> 6                  row6     col1     0
#> 7                  row7     col1     0
#> 8                  row8     col1     0
#> 9                  row9     col1     0
#> 10                 row1     col2     0
#> 11                 row2     col2     1
#> 12                 row3     col2     0
#> 13                 row4     col2     0
#> 14                 row5     col2     0
#> 15                 row6     col2     0
#> 16                 row7     col2     0
#> 17                 row8     col2     0
#> 18                 row9     col2     0
#> 19                 row1     col3     0
#> 20                 row2     col3     0
#> 21                 row3     col3     0
#> 22                 row4     col3     0
#> 23                 row5     col3     0
#> 24                 row6     col3     0
#> 25                 row7     col3     0
#> 26                 row8     col3     0
#> 27                 row9     col3     1
#> 28                 row1     col4     1
#> 29                 row2     col4     0
#> 30                 row3     col4     0
#> 31                 row4     col4     0
#> 32                 row5     col4     0
#> 33                 row6     col4     0
#> 34                 row7     col4     0
#> 35                 row8     col4     0
#> 36                 row9     col4     0
#> 37                 row1     col5     0
#> 38                 row2     col5     0
#> 39                 row3     col5     0
#> 40                 row4     col5     0
#> 41                 row5     col5     0
#> 42                 row6     col5     0
#> 43                 row7     col5     0
#> 44                 row8     col5     1
#> 45                 row9     col5     0
#> 46                 row1     col6     0
#> 47                 row2     col6     0
#> 48                 row3     col6     0
#> 49                 row4     col6     0
#> 50                 row5     col6     0
#> 51                 row6     col6     1
#> 52                 row7     col6     0
#> 53                 row8     col6     0
#> 54                 row9     col6     0
#> 55                 row1     col7     0
#> 56                 row2     col7     0
#> 57                 row3     col7     0
#> 58                 row4     col7     0
#> 59                 row5     col7     1
#> 60                 row6     col7     0
#> 61                 row7     col7     0
#> 62                 row8     col7     0
#> 63                 row9     col7     0
#> 64                 row1     col8     0
#> 65                 row2     col8     0
#> 66                 row3     col8     0
#> 67                 row4     col8     0
#> 68                 row5     col8     0
#> 69                 row6     col8     0
#> 70                 row7     col8     1
#> 71                 row8     col8     0
#> 72                 row9     col8     0

Created on 2021-11-07 by the reprex package (v2.0.0)