wen - 1 month ago 9

R Question

Let say I have a frequency table:

`t = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))`

t

yes no

yes 20 5

no 10 15

I want to convert the frequency table back to raw data. My code is (which is not working):

`a = rep(c("yes","no"), colSums(t(t)))`

b = rep(c("yes","no"), colSums(t))

table(a,b)

b

a no yes

no 20 5

yes 0 25

Can someone tell me what is wrong? Thanks in advance!

Answer

```
mat <- matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))
a <- rep(rep(c("yes", "no"), 2), c(mat))
b <- rep(c("yes", "no"), colSums(mat))
a <- factor(a, levels=c("yes", "no"))
b <- factor(b, levels=c("yes", "no"))
table(a, b)
b
a yes no
yes 20 5
no 10 15
```