gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:33:39 24 4
gpt4 key购买 nike

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

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

我尝试得到一个频率表如下:
table(tmp2[,1:3], tmp2[,4:6])

但我得到:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?



预期输出:

enter image description here

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

最佳答案

一个选项:

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

如果您有不同数量的 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]))

关于r - R中多个列和多行的表频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36595016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com