r - Sort a contingency table without changing table class -
i want reorder factors of cont.table
test.a <- c(rep(1,20),rep(0,40)) test.b <- c(rep(1,25),rep(0,35)) cont.table <- addmargins(table(test.a, test.b)) test.b test.a 0 1 sum 0 35 5 40 1 0 20 20 sum 35 25 60
i want able order factors 0 , 1. result want this
1 0 sum 1 20 0 20 0 5 35 40 sum 25 35 60
i did this, lose class table, needed me
> tbl <- as.data.frame.matrix(addmargins(table(test.a, test.b))) > tbl2 <- cbind(tbl[2],tbl[1],tbl[3]) > tblfinal <- rbind(tbl2[2,],tbl2[1,],tbl2[3,]) > as.table(tblfinal) error in as.table.default(tblfinal) : cannot coerce table
is there possible way? simpler better
make test.x objects factors defined order, tables etc sorted appropriately. e.g.:
test.a <- factor(test.a,levels=c(1,0)) test.b <- factor(test.b,levels=c(1,0)) addmargins(table(test.a,test.b)) # test.b #test.a 1 0 sum # 1 20 0 20 # 0 5 35 40 # sum 25 35 60
Comments
Post a Comment