我创建了一个表,如下所示:
mymatrix3=matrix(c(29,54,44,77,62,131,36,67), nrow=4, ncol = 2, byrow = TRUE)
colnames(mymatrix3) <- c("Right Direction", "Wrong Direction")
rownames(mymatrix3) <-c("Northeast", "North Central", "South", "West")
mymatrix3
sample_dist = as.array(prop.table(mymatrix3,2))
它给了我:mymatrix3,这是不同地区的人们对政府方向的看法:
Right Direction Wrong Direction
Northeast 29 54
North Central 44 77
South 62 131
West 36 67
sample_dist:
Right Direction Wrong Direction
Northeast 0.1695906 0.1641337
North Central 0.2573099 0.2340426
South 0.3625731 0.3981763
West 0.2105263 0.2036474
但是,我想要的是表的行空白处两列的总和。我不想分开一群人。我想汇总它们,然后计算道具。我该怎么办?
答案 0 :(得分:1)
如果您只需要列的总行数(应与任意数量的列一起使用),那该怎么办?
cbind(sample_dist, total = rowSums(sample_dist))
Right Direction Wrong Direction total
Northeast 0.1695906 0.1641337 0.3337244
North Central 0.2573099 0.2340426 0.4913525
South 0.3625731 0.3981763 0.7607494
West 0.2105263 0.2036474 0.4141737
如果您需要他们的总重量:
cbind(sample_dist, total = rowSums(sample_dist), perc = prop.table(rowSums(sample_dist)))
Right Direction Wrong Direction total perc
Northeast 0.1695906 0.1641337 0.3337244 0.1668622
North Central 0.2573099 0.2340426 0.4913525 0.2456762
South 0.3625731 0.3981763 0.7607494 0.3803747
West 0.2105263 0.2036474 0.4141737 0.2070869
答案 1 :(得分:1)
您可以对行求和,然后计算表:
prop.table(rowSums(mymatrix3,2))
> Northeast North Central South West
0.166 0.242 0.386 0.206