I have two columns with levels that are nested. By nested I mean that the factors in column 2 are dependent upon the values of column 1. I would like to order the factor levels in column 1 alphabetically. I would like the factor levels in column 2 to be alphabetically ordered after the alphabetical order of column 1.
data<-as.data.frame(cbind(c("A","B","D","C","A","B","D","C","A","B","C","D"),
c("Alpha","Beta","Gamma","Delta","Zeta","Chi","Omega",
"Delta","Alpha","Gamma","Beta","Zeta")))
data<-data[with(data, order(factor(levels(data$V1))),
factor(levels(data$V2))), ]
I tried to order the factor levels as described above, but the second column is not ordered after the first; it is just alphabetically ordered on its own. I know I can manually type in the order of levels that I desire, but I am after something that doesn't require that.
Thank you.
order(V1,V2)
> The effect you are seeing comes from the fact thatlevels(fac)
is generally a lot shorter thanfac
itself. – IRTFMdata<-data[order(data$V1,data$V2), ]
– Metrics