开发者

handling high dimension tables

开发者 https://www.devze.com 2023-03-02 19:18 出处:网络
I have a table that I routinely compute with R that has three dimensions. I would like to add some tables within the (here 5) marginal tables. What I usually do is like:

I have a table that I routinely compute with R that has three dimensions. I would like to add some tables within the (here 5) marginal tables. What I usually do is like:

    A=sample(LETTERS[1:5],100, rep=T)

    b=sample(letters[1:2],100, rep=T)

    numbers=sample(1:3,100, rep=T)

   ( tab=table(A,b,numbers) )
   ( tab1=ftable(addmargins(tab)) )

I would like to add the sum of the first few marginal tables and then the sum of the remaining tables at the bottom, then the grand total. I can imagine that in the resulting ftable I would want the As,Bs,Cs, then the sum of those three, then the Ds, Es, and the sum of those two, then the sum of all of the tables, like:

         numbers   1   2   3 Sum
    A   b                          
    A   a             1   5   0   6
        b             4   2   2   8
        Sum           5   7   2  14
    B   a             2   6   6  14
        b             5   4   5  14
        Sum           7  10  11  28
    C   a             3   2   5  10
        b             1   2   2   5
        Sum           4   4   7  15
  sumac a             6  13  11  30   #### how do i add these three lines
        b               ....
        sum             ....
    D   a             2   1   1   4
        b             4   5开发者_开发百科   3  12
        Sum           6   6   4  16
    E   a             5   3   4  12
        b             4   3   8  15
        Sum           9   6  12  27
  sumde a             7   4   5  20   #### and these
        b               ....
        sum               ....
  sumae a            13  17  16  46
        b            18  16  20  54
        Sum          31  33  36 100

As usual I think the solution is prolly many fewer lines than the question. Thanks Seth Latimer


You could do something like this:

isABC<-ifelse(A %in% c("A", "B", "C"), "ABC", "CD")
( tab=table(isABC,A,b,numbers) )
( tab1=ftable(addmargins(tab)) )

Now you have a larger table which holds even more rows than you want, but those should be easy to drop...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号