Hello, I want to create contingency table. The data which should be presented in contingency table look like this.(Its a very long list. I am copying small part of it) obs1 obs2 exp1 exp2 3 8 725 875 0 0 58 70 3 7 435 525 10 7 754 910 0 1 145 175 the table should be like this. ----------|-----------| obs1 | obs2 | ----------|-----------| exp1 | exp2 | ----------------------- I have tried to use ftable function. But it didn't work. Please help me. Regards Reena -- View this message in context: http://r.789695.n4.nabble.com/contingency-table-tp4217741p4217741.html Sent from the R help mailing list archive at Nabble.com.
contingency table
4 messages · reena, Marc Schwartz, Rolf Turner
This is my list. obs1 obs2 exp1 exp2 3 8 725 875 0 0 58 70 3 7 435 525 10 7 754 910 0 1 145 175 and i want result in contingency table as obs 3 8 exp 725 875 next table will be obs 0 0 exp 58 70 and so on... -- View this message in context: http://r.789695.n4.nabble.com/contingency-table-tp4217741p4218845.html Sent from the R help mailing list archive at Nabble.com.
On Dec 20, 2011, at 12:36 PM, reena wrote:
This is my list. obs1 obs2 exp1 exp2 3 8 725 875 0 0 58 70 3 7 435 525 10 7 754 910 0 1 145 175 and i want result in contingency table as obs 3 8 exp 725 875 next table will be obs 0 0 exp 58 70 and so on...
Hi, It's not clear what you intend to do with the data after restructuring, but creating a list of the 2x2 tables from each row seems a reasonable first step. Thus, if your data above is contained in a data frame 'DF':
DF
obs1 obs2 exp1 exp2 1 3 8 725 875 2 0 0 58 70 3 3 7 435 525 4 10 7 754 910 5 0 1 145 175 # Iterate over each row in DF, returning a 2x2 matrix from each
lapply(seq(nrow(DF)), function(i) matrix(DF[i, ], 2, 2, byrow = TRUE))
[[1]]
[,1] [,2]
[1,] 3 8
[2,] 725 875
[[2]]
[,1] [,2]
[1,] 0 0
[2,] 58 70
[[3]]
[,1] [,2]
[1,] 3 7
[2,] 435 525
[[4]]
[,1] [,2]
[1,] 10 7
[2,] 754 910
[[5]]
[,1] [,2]
[1,] 0 1
[2,] 145 175
This gives you a list of 5 2x2 matrices, one from each row in DF. See ?lapply, ?seq and ?matrix.
HTH,
Marc Schwartz
Do you actually know what "contingency table" means?
The tables in your example make no sense at all as contingency
tables *especially if "obs" means "observed" and "exp" means
"expected".
You can, however, extra the tables in the manner which you seem
to desire, as follows: Let your data object be called "dat". (And
surely this object is either a data frame or a matrix --- judging by
the display --- and *NOT* a list. Learn to use correct terminology
and you will be much more likely to get useful replies to your questions.)
dat <- as.matrix(dat) # To make sure it's a matrix r.t. a data frame.
xxx <- lapply(1:nrow(dat),
function(i,x){matrix(x[i,],ncol=2,byrow=TRUE)},x=dat)
Now "xxx" is a (genuine!) list, and the i-th entry of this list is the
i-th table from
your "required" set of tables. E.g.:
xxx[[3]]
[,1] [,2]
[1,] 3 7
[2,] 435 525
cheers,
Rolf Turner
On 21/12/11 07:36, reena wrote:
This is my list. obs1 obs2 exp1 exp2 3 8 725 875 0 0 58 70 3 7 435 525 10 7 754 910 0 1 145 175 and i want result in contingency table as obs 3 8 exp 725 875 next table will be obs 0 0 exp 58 70 and so on... -- View this message in context: http://r.789695.n4.nabble.com/contingency-table-tp4217741p4218845.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.