如何根据另一个对象的row.names对data.frame(表)进行排序

时间:2014-05-18 23:52:02

标签: r sorting dataframe

我必须编写一个报表,根据另一个表的频率对交叉表进行排序。我使用以下代码构建第一个表:

library(descr)

X <- c(rep('Anthony', 6), rep('Marcelo', 4), rep('Luiz', 3), rep('Lind', 2), rep('Cesar', 1), rep('Outros', 6), rep('Ninguém', 6), rep('NS/NR', 6)) 
PESO <- rep(1, length(X))
REG <- sample(c('R1','R2','R3','R4'), length(X), replace=T)
bd <- data.frame(X, PESO, REG)

perc <- (round(freq(bd$X, w=bd$PESO, plot=F), digits=0))
perc <- perc[rownames(perc)!="NA's" & rownames(perc)!='NR' & rownames(perc)!='Total',]
perc <- perc[,-1]
ns <- perc[names(perc)=='NS/NR']
ni <- perc[names(perc)=="Ninguém"]
ou <- perc[names(perc)=="Outros"]
perc <- data.frame('%'=sort(perc[names(perc)!='NS/NR' & names(perc)!="Ninguém" & names(perc)!="Outros"], decreasing=T))
perc <- rbind(perc,ou,ni,ns)
rownames(perc)[rownames(perc)==6 | rownames(perc)==7 | rownames(perc)==8] <- c('Outros', 'Ninguém', 'NS/NR')
perc <- rbind(perc, Total=100, Base=dim(bd)[1])

结果是

         X.
Anthony  18
Marcelo  12
Luiz      9
Lind      6
Cesar     3
Outros   18
Ninguém  18
NS/NR    18
Total   100
Base     34

现在,当我运行代码与另一个变量交叉时,我得到

tab <- round(rbind(prop.table(xtabs(PESO~X+REG, bd),margin=2)*100))
ns <- tab[rownames(tab)=='NS/NR',]
ni <- tab[rownames(tab)=="Ninguém",]
ou <- tab[rownames(tab)=="Outros",]
tab <- tab[rownames(tab)!='NS/NR' & rownames(tab)!="Ninguém" & rownames(tab)!="Outros",]
tab <- tab[order(tab[,1], decreasing=T),]
perc <- rbind(tab,ou,ni,ns,Total=100,Base=apply(xtabs(PESO~X+REG, bd), 2, sum))
rownames(perc)[rownames(perc)=='ou' | rownames(perc)=='ni' | rownames(perc)=='ns'] <- c('Outros', 'Ninguém', 'NS/NR')
perc <- perc[,colnames(perc)!='Total']

结果是

         R1  R2  R3  R4
Anthony  17  14  25  11
Luiz     17  14   0  11
Marcelo  17   0  17  11
Cesar     0   0   0  11
Lind      0   0  17   0
Outros    0  43  17  11
Ninguém  33  14  17  11
NS/NR    17  14   8  33
Total   100 100 100 100
Base      6   7  12   9

我想让这个表与第一个表具有相同的顺序。像:

         R1  R2  R3  R4
Anthony  17  14  25  11
Marcelo  17   0  17  11
Luiz     17  14   0  11
Lind      0   0  17   0
Cesar     0   0   0  11 
Outros    0  43  17  11
Ninguém  33  14  17  11
NS/NR    17  14   8  33
Total   100 100 100 100
Base      6   7  12   9

我如何调整我的代码来解决这个问题?

感谢

1 个答案:

答案 0 :(得分:1)

在第一个块捕获行顺序后

perc <- rbind(perc, Total=100, Base=dim(bd)[1])
#add
perc.order <- rownames(perc)

然后,您可以使用这些行名称对最终数据进行排序。所以

之后
perc <- perc[,colnames(perc)!='Total']
#add
perc[perc.order, ]

只要将元素作为字符向量传递,就可以使用这些名称索引具有行名称的元素。这只会返回第一个表中的值。如果添加了新值,它们将不会包含在输出中。