André Júng
2011-May-08 15:10 UTC
[R] Rearranging variables in table in non-alphabetical (manually specified) order
Dear all, I'm trying to rearrange variables in a table in a custum order for using it with levelplot. So far I could only find examples showing how to sort alphabetically. Here is a short example:? a <- c("Anna","Anna","Michael","Klaus","Klaus","Anna","Fritz")? b <- c("Schnitzel","Pommes","Pommes","Schnitzel","Wurst","Schnitzel","Schnitzel")? food <- matrix(c(a,b),7) as.data.frame(food) -> tmp? as.data.frame(table(tmp)) -> X levelplot(X$Freq ~ X$V1 * X$V2,xlab="people",ylab="food")? Food is now ordered: Pommes, Schnitzel, Wurst. But I need: Schnitzel, Pommes, Wurst.? How can I define the order? I'm happy about every suggestion.? Thanks in advance!? Andre? ? ___________________________________________________________ Schon geh?rt? WEB.DE hat einen genialen Phishing-Filter in die
Duncan Murdoch
2011-May-08 16:24 UTC
[R] Rearranging variables in table in non-alphabetical (manually specified) order
On 11-05-08 11:10 AM, Andr? J?ng wrote:> Dear all, > > I'm trying to rearrange variables in a table in a custum order for using it with levelplot. So far I could only find examples showing how to sort alphabetically. Here is a short example: > > a<- c("Anna","Anna","Michael","Klaus","Klaus","Anna","Fritz") > > b<- c("Schnitzel","Pommes","Pommes","Schnitzel","Wurst","Schnitzel","Schnitzel") > food<- matrix(c(a,b),7) > as.data.frame(food) -> tmp > as.data.frame(table(tmp)) -> X > levelplot(X$Freq ~ X$V1 * X$V2,xlab="people",ylab="food") > > Food is now ordered: Pommes, Schnitzel, Wurst. > But I need: Schnitzel, Pommes, Wurst. > > How can I define the order? I'm happy about every suggestion. >The general answer to that question is to make b into a factor with the levels in a specified order, e.g. by b <- factor(b, levels=c("Schnitzel", "Pommes", "Wurst")) However, this doesn't survive the other strange things you are doing. If you simplify the rest, it should be fine. For example: food <- data.frame(a,b) X <- as.data.frame(table(food)) levelplot(Freq ~ a * b, data= X) Duncan Murdoch
Uwe Ligges
2011-May-08 16:33 UTC
[R] Rearranging variables in table in non-alphabetical (manually specified) order
On 08.05.2011 17:10, Andr? J?ng wrote:> Dear all, > > I'm trying to rearrange variables in a table in a custum order for using it with levelplot. So far I could only find examples showing how to sort alphabetically. Here is a short example: > > a<- c("Anna","Anna","Michael","Klaus","Klaus","Anna","Fritz") > > b<- c("Schnitzel","Pommes","Pommes","Schnitzel","Wurst","Schnitzel","Schnitzel")Hmmm, why don't you ask your supervisor about your homework? I wonder who your supervisor is. Anyway, some hint: Replace b by a factor with levels in the desired order.> food<- matrix(c(a,b),7) > as.data.frame(food) -> tmpPlease replace the two lines above by something like food <- data.frame(names=a, food=b) beacuse 1. your matrix will destroy the factor again and is not required at all, and 2. never ever use "->" which makes your code almost unreadable. Uwe Ligges> as.data.frame(table(tmp)) -> X > levelplot(X$Freq ~ X$V1 * X$V2,xlab="people",ylab="food") > > Food is now ordered: Pommes, Schnitzel, Wurst. > But I need: Schnitzel, Pommes, Wurst. > > How can I define the order? I'm happy about every suggestion. > > Thanks in advance! > Andre > > > > ___________________________________________________________ > Schon geh?rt? WEB.DE hat einen genialen Phishing-Filter in die > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.