-1

I have a data like this:

A  B
1 10 
1 20
1 30
2 10
2 30
2 40
3 20
3 10
3 30
4 20
4 10
5 10
5 10

and I want to build a contingency table like this:

   10 20 30 40
10 1   3  2  0
20 3   0  2  0
30 2   2  0  0
40 0   0  0  0

Meaning: According to column A, for each two values of column B mark + 1 in the specific Contingency table.

Can you help me do this?

4

1 回答 1

0

这是一个非常丑陋的答案,使用图像中的数据,因为我已经在您的问题上花费了太多时间。一般来说,让你的结果取决于变量的顺序是不切实际的。

A <- rep(c(1:4),c(3,2,3,3))
B <- c(10,10,30,10,20,30,20,10,10,20,30)
data <- data.frame(cbind(A,B))

#split by A
library(plyr)
data2 <- ddply(data,.(A),function(x){
  combined_pairs <- cbind(x$B[-nrow(x)],
                          x$B[-1])
  #return data where first is always lowest
  smallest <- apply(combined_pairs,MARGIN=1,
                    FUN=min)
  largest <- apply(combined_pairs,MARGIN=1,
                   FUN=max)
  return(data.frame(small=smallest,large=largest))
})


library(reshape2)

result <- dcast(small~large,data=data2, 
                fun.aggregate=length) 
> result
  small 10 20 30
1    10  1  3  1
2    20  0  0  2

如果您仍然需要它们,我认为您可以自己添加空行。

于 2015-07-20T19:26:16.607 回答