开发者

Summary by Row of a Categorical Variable in R

I 开发者_JAVA百科have the following matrix

coin.flip<-expand.grid(c("H","T"),c("H","T"),c("H","T"),c("H","T"))

> coin.flip
   Var1 Var2 Var3 Var4
1     H    H    H    H
2     T    H    H    H
3     H    T    H    H
4     T    T    H    H
5     H    H    T    H
6     T    H    T    H
7     H    T    T    H
8     T    T    T    H
9     H    H    H    T
10    T    H    H    T
11    H    T    H    T
12    T    T    H    T
13    H    H    T    T
14    T    H    T    T
15    H    T    T    T
16    T    T    T    T

I would like to get a row by row summary of the counts of H's and T's. I would also like a table of how many row's had 1 T, 2 T's, etc...

Thanks a lot! I've been searching for this answer for a little bit and haven't found anything yet.


> coin.flip$total_H <- rowSums(coin.flip=="H")
> coin.flip
   Var1 Var2 Var3 Var4 total_H
1     H    H    H    H       4
2     T    H    H    H       3
3     H    T    H    H       3
4     T    T    H    H       2
5     H    H    T    H       3
....snipped
> table(coin.flip$total_H)

0 1 2 3 4 
1 4 6 4 1    

Total T's table is just
> table(4- coin.flip$total_H )

0 1 2 3 4 
1 4 6 4 1     # boringly similar to total H table


Here's an way using apply and then table:

rs <- t(apply(coin.flip,1,function(x){c(length(which(x=='H')),length(which(x=='T')))}))
table(rs[,2])

I transposed the results from apply since that's the way you probably expect them displayed.


Strictly coin.flip is a data.frame not a matrix.

You could use DWin's solution. Another approach is

> summary(t(coin.flip))
 V1    V2    V3    V4    V5    V6    V7    V8    V9    V10   V11   V12   V13   V14   V15   V16  
 H:4   H:3   H:3   H:2   H:3   H:2   H:2   H:1   H:3   H:2   H:2   H:1   H:2   H:1   H:1   T:4  
       T:1   T:1   T:2   T:1   T:2   T:2   T:3   T:1   T:2   T:2   T:3   T:2   T:3   T:3  

To get the table quickly, combine the answers from DWin and joran

> table(rowSums(coin.flip=="T"))

0 1 2 3 4 
1 4 6 4 1   
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜