1

Is possible to create fake data to recreate a contingency table?

For example:

originalTable <- matrix(c(188, 29, 20, 51), ncol = 2, byrow = TRUE)
colnames(originalTable) <- c("A", "B")
rownames(originalTable) <- c("C", "D")

Is possible to generate a dataframe from the table with 288 pairs of observations that match the table?

I found the r2dtable function, but any idea how to extract or save as dataframe?

r2dtable(1, c(217, 71), c(208, 80))

Thanks in advance

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
sergiouribe
  • 221
  • 1
  • 3
  • 11
  • After the `data.frame(as.table(originalTable))` step, you find may alternatives here: [Replicate each row of data.frame and specify the number of replications for each row](https://stackoverflow.com/questions/2894775/replicate-each-row-of-data-frame-and-specify-the-number-of-replications-for-each) – Henrik Mar 04 '18 at 08:44
  • 1
    Another: [Finding the original vectors from an interaction table](https://stackoverflow.com/questions/19841768/finding-the-original-vectors-from-an-interaction-table) – Henrik Mar 04 '18 at 11:44

3 Answers3

2

You can just use the table to generate the correct number of pairs.

x = c()
for(row in rownames(originalTable)) {
    for(col in colnames(originalTable)) {
        x = rbind(x, matrix(rep(c(row, col), originalTable[row,col]), ncol=2, byrow=TRUE))
    }
}

df = as.data.frame(x)
table(df)
   V2
V1    A   B
  C 188  29
  D  20  51
G5W
  • 36,531
  • 10
  • 47
  • 80
  • great, that's exactly was I was searching! I was trying to recreate some datasets that I found in textbooks and papers. Thanks again! – sergiouribe Mar 04 '18 at 03:24
2

You can use expandRows from my "splitstackshape" package:

library(splitstackshape)
expandRows(data.frame(as.table(originalTable)), "Freq")
#       Var1 Var2
# 1        C    A
# 1.1      C    A
# 1.2      C    A
# 1.3      C    A
# 1.4      C    A
# -----
# 1.19     C    A
# 1.20     C    A
# 1.21     C    A
# 1.22     C    A
# 1.23     C    A
# 1.24     C    A
# 1.25     C    A
# 1.26     C    A
# 1.27     C    A
# -----
# 4.43     D    B
# 4.44     D    B
# 4.45     D    B
# 4.46     D    B
# 4.47     D    B
# 4.48     D    B
# 4.49     D    B
# 4.50     D    B

nrow(.Last.value)
# [1] 288
sum(originalTable)
# [1] 288

You won't need the as.table if you're already dealing with an actual table.

Of course, you can also do it without a package:

data.frame(as.table(originalTable))[rep(sequence(prod(dim(originalTable))), 
                                        c(originalTable)), c(1, 2)]
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
0

Try:

ctab <- as.table(matrix(c(12, 6, 6, 22), ncol=2))
ctab
wtab <- as.data.frame(ctab)
wtab
index <- rep(1:nrow(wtab), wtab$Freq)
utab  <- wtab[index,]
utab
sigbert
  • 77
  • 5