stefan.duke at gmail.com
2010-Jun-08 13:42 UTC
[R] constructing a contingency table (ftable, table)
Dear all, an hopefully quick table question. I have the following data: Two objects that are 2*9 matrix with nine column names (Dis1, ..., Dis9) and the row names (2010,2020). The content are frequencies (numeric). In want to create a table that is along the lines of ftable(UCBAdmissions) and should looks like this: Dis1 | ...| Dis9 2010|2020|....|2010|2020 (first row,first column is the value of Dis1 in 2010 from first object)| (first row,second column is the value of Dis1 in 2020 from first object)| .... (second row,first column is the value of Dis1 in 2010 from second object)| (first row,second column is the value of Dis1 in 2020 from second object)| .... and so on So basically what ftable does. But I do not understand how I can turn my two matrices (which already contain the frequencies) into the appropriate table object (if thats the way to go). Thanks and best, Stefan
I could get something close to what you asked using a little hack,
emulating a table using an array based on your two matrices :
test <- matrix(rpois(18,10),ncol=9,nrow=2)
colnames(test) <- paste("Dis",1:9,sep="")
rownames(test) <- c("2010","2020")
test2 <- matrix(rpois(18,10),ncol=9,nrow=2)
colnames(test2) <- paste("Dis",1:9,sep="")
rownames(test2) <- c("2010","2020")
tmp <- array(cbind(test,test2),
dim=c(2,9,2),
dimnames=list(rownames(test),colnames(test),c("Test","Test2")))
ftable(as.table(tmp))
Cheers
Joris
On Tue, Jun 8, 2010 at 3:42 PM, stefan.duke at gmail.com
<stefan.duke at gmail.com> wrote:> Dear all,
> an hopefully quick table question.
>
> I have the following data:
> Two objects that are 2*9 matrix with nine column names (Dis1, ...,
> Dis9) and the row names (2010,2020). The content are frequencies
> (numeric).
>
> In want to create a table that is along the lines of
> ftable(UCBAdmissions) and should looks like this:
> Dis1 | ...| Dis9
> 2010|2020|....|2010|2020
> (first row,first column is the value of Dis1 in 2010 from first
> object)| (first row,second column is the value of Dis1 in 2020 from
> first object)| ....
> (second row,first column is the value of Dis1 in 2010 from second
> object)| (first row,second column is the value of Dis1 in 2020 from
> second object)| ....
> and so on
>
> So basically what ftable does. But I do not understand how I can turn
> my two matrices (which already contain the frequencies) into the
> appropriate table object (if thats the way to go).
>
> Thanks and best,
> Stefan
>
> ______________________________________________
> 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.
>
--
Joris Meys
Statistical consultant
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control
tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
stefan.duke at gmail.com
2010-Jun-08 18:47 UTC
[R] constructing a contingency table (ftable, table)
Hi Joris,
thanks for your help. I just had to alter it slightly (basically just
transposing):
tmp <- array(rbind(t(test),t(test2)),
dim=c(9,2,2),
dimnames=list(colnames(test),rownames(test),c("Test","Test2")))
ftable(tmp)
Thanks again!
Best,
Stefan
On Tue, Jun 8, 2010 at 5:46 PM, Joris Meys <jorismeys at gmail.com>
wrote:> I could get something close to what you asked using a little hack,
> emulating a table using an array based on your two matrices :
>
> test <- matrix(rpois(18,10),ncol=9,nrow=2)
> colnames(test) <- paste("Dis",1:9,sep="")
> rownames(test) <- c("2010","2020")
>
> test2 <- matrix(rpois(18,10),ncol=9,nrow=2)
> colnames(test2) <- paste("Dis",1:9,sep="")
> rownames(test2) <- c("2010","2020")
>
> tmp <- array(cbind(test,test2),
> ? ? ? ?dim=c(2,9,2),
> ? ? ?
?dimnames=list(rownames(test),colnames(test),c("Test","Test2")))
>
> ftable(as.table(tmp))
>
> Cheers
> Joris
>
> On Tue, Jun 8, 2010 at 3:42 PM, stefan.duke at gmail.com
> <stefan.duke at gmail.com> wrote:
>> Dear all,
>> an hopefully quick table question.
>>
>> I have the following data:
>> Two objects that are 2*9 matrix with nine column names (Dis1, ...,
>> Dis9) and the row names (2010,2020). The content are frequencies
>> (numeric).
>>
>> In want to create a table that is along the lines of
>> ftable(UCBAdmissions) and should looks like this:
>> Dis1 | ...| Dis9
>> 2010|2020|....|2010|2020
>> (first row,first column is the value of Dis1 in 2010 from first
>> object)| (first row,second column is the value of Dis1 in 2020 from
>> first object)| ....
>> (second row,first column is the value of Dis1 in 2010 from second
>> object)| (first row,second column is the value of Dis1 in 2020 from
>> second object)| ....
>> and so on
>>
>> So basically what ftable does. But I do not understand how I can turn
>> my two matrices (which already contain the frequencies) into the
>> appropriate table object (if thats the way to go).
>>
>> Thanks and best,
>> Stefan
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joris Meys
> Statistical consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> tel : +32 9 264 59 87
> Joris.Meys at Ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>