假设您有 2 个矩阵m1
,m2
并且每个矩阵都有相等的列数。
m1 = matrix(0, 10, 5, dimnames = list(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), c(1, 2, 3, 4, 5)))
m1[1,] = c(0,0,0,0,1)
m1[2,] = c(0,0,0,1,1)
m1[3,] = c(0,0,1,1,1)
m1[4,] = c(0,0,1,1,0)
m1[5,] = c(1,0,0,0,0)
m1[6,] = c(1,1,1,0,0)
m1[7,] = c(0,1,1,0,0)
m1[8,] = c(0,1,1,0,0)
m1[9,] = c(0,1,1,1,0)
m1[10,] = c(1,1,1,0,1)
m2 = matrix(0, 10, 5, dimnames = list(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), c(1, 2, 3, 4, 5)))
m2[1,] = c(0,0,0,0,1)
m2[2,] = c(0,0,0,1,1)
m2[3,] = c(0,0,1,1,1)
m2[4,] = c(0,0,1,1,0)
m2[5,] = c(1,0,0,0,0)
m2[6,] = c(1,1,1,0,0)
m2[7,] = c(0,1,1,0,0)
m2[8,] = c(0,1,1,0,0)
m2[9,] = c(0,1,1,1,0)
m2[10,] = c(1,1,1,0,1)
我想看到的是这两个矩阵的饼图比较。
我能想到的一种方法是为每一列添加每一行,然后使用这些来获取饼图的分数。
sumcols <-function(x){
for (i in 1:numcols(x)){
sum <- sum(x[,i])
sums.append(sum) #python here ...
}
return(sums)
}
所以现在,我可以传递任何矩阵,取回总和列表,我假设现在我们可以使用它来获取饼图:
sums1 <- sumcols(m1)
sums2 <- sumcols(m2)
par(mfrow = c(1,2))
pie(c(sums1,sums2))
感谢您的帮助!