R中多个列和多行的表频率

S12*_*000 5 r frequency

我正在尝试从此数据框中获取频率表:

tmp2 <- structure(list(a1 = c(1L, 0L, 0L), a2 = c(1L, 0L, 1L),
                       a3 = c(0L, 1L, 0L), b1 = c(1L, 0L, 1L),
                       b2 = c(1L, 0L, 0L), b3 = c(0L, 1L, 1L)),
                       .Names = c("a1", "a2", "a3", "b1", "b2", "b3"),
                       class = "data.frame", row.names = c(NA, -3L))


tmp2 <- read.csv("tmp2.csv", sep=";")
tmp2
> tmp2
  a1 a2 a3 b1 b2 b3
1  1  1  0  1  1  0
2  0  0  1  0  0  1
3  0  1  0  1  0  1
Run Code Online (Sandbox Code Playgroud)

我尝试得到一个频率表如下:

table(tmp2[,1:3], tmp2[,4:6])
Run Code Online (Sandbox Code Playgroud)

但我得到:

sort.list(y) 中的错误:对于 'sort.list','x' 必须是原子的
您是否在列表上调用了 'sort'?

预期输出:

在此处输入图片说明

信息:不需要方阵,例如我应该能够添加 b4 b5 并保留 a1 a2 a3

nic*_*ola 5

一个选项:

matrix(colSums(tmp2[,rep(1:3,3)] & tmp2[,rep(4:6,each=3)]),
       ncol=3,nrow=3,
       dimnames=list(colnames(tmp2)[1:3],colnames(tmp2)[4:6]))
#   b1 b2 b3
#a1  1  1  0
#a2  2  1  1
#a3  0  0  1
Run Code Online (Sandbox Code Playgroud)

如果你有不同数量的ab列,您可以试试:

acols<-1:3 #state the indices of the a columns
bcols<-4:6 #same for b; if you add a column this should be 4:7
matrix(colSums(tmp2[,rep(acols,length(bcols))] & tmp2[,rep(bcols,each=length(acols))]),
           ncol=length(bcols),nrow=length(acols),
           dimnames=list(colnames(tmp2)[acols],colnames(tmp2)[bcols]))
Run Code Online (Sandbox Code Playgroud)