Re: [R] Rearranging variables in table in non-alphabetical (manually specified) order

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Sun, 08 May 2011 12:24:11 -0400

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



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. Received on Sun 08 May 2011 - 17:28:49 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 08 May 2011 - 18:40:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive