Renger van Nieuwkoop
2013-Oct-08 17:29 UTC
[R] tables with row sorted numerically although factors
Hi I am using the package tables and want to have the rows in the numerical order and not in the alphabetical order: library(tables) Nodes <- c(1,10,20,2) Values <- c(1,2,3,4) Data <- data.frame(cbind(Nodes,Values)) data$Nodes<- as.factor(as.character(data$Nodes)) # necessary to get factors for tabular tabular(Nodes ~ Values*mean, data=data) Values Nodes mean 1 1 10 2 2 4 20 3 And what I want is this: Values Nodes mean 1 1 2 4 10 2 20 3 Any idea how to do this? (the solution is not to write 01, 02, 10, 20, because I use Nodes in lot of places elsewhere, where I can't use 01, etc.) Cheers Renger _________________________________________ Renger van Nieuwkoop Centre of Economic Research (CER-ETH) Zürichbergstrasse 18 (ZUE) CH - 8032 Zürich +41 44 632 02 63 mailto: rengerv@etzh.ch blog.modelworks.ch [[alternative HTML version deleted]]
Rui Barradas
2013-Oct-08 17:53 UTC
[R] tables with row sorted numerically although factors
Hello, First of all, there's no need for data.frame(cbind(...)). data.frame() only will do the job, and it's less error prone. As for the question, since the column Nodes is to become a factor, why use as.character()? Without it the problem is solved: data <- data.frame(Nodes,Values) data$Nodes<- factor(data$Nodes) # necessary to get factors for tabular tabular(Nodes ~ Values*mean, data=data) Hope this helps, Rui Barradas Em 08-10-2013 18:29, Renger van Nieuwkoop escreveu:> Hi > I am using the package tables and want to have the rows in the numerical order and not in the alphabetical order: > > library(tables) > Nodes <- c(1,10,20,2) > Values <- c(1,2,3,4) > Data <- data.frame(cbind(Nodes,Values)) > data$Nodes<- as.factor(as.character(data$Nodes)) # necessary to get factors for tabular > > tabular(Nodes ~ Values*mean, data=data) > > Values > Nodes mean > 1 1 > 10 2 > 2 4 > 20 3 > > And what I want is this: > > Values > Nodes mean > 1 1 > 2 4 > 10 2 > 20 3 > > Any idea how to do this? (the solution is not to write 01, 02, 10, 20, because I use Nodes in lot of places elsewhere, where I can't use 01, etc.) > > Cheers > > Renger > > > > > _________________________________________ > Renger van Nieuwkoop > Centre of Economic Research (CER-ETH) > Z?richbergstrasse 18 (ZUE) > CH - 8032 Z?rich > +41 44 632 02 63 > mailto: rengerv at etzh.ch > blog.modelworks.ch > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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. >
David Winsemius
2013-Oct-08 17:57 UTC
[R] tables with row sorted numerically although factors
On Oct 8, 2013, at 10:29 AM, Renger van Nieuwkoop wrote:> Hi > I am using the package tables and want to have the rows in the numerical order and not in the alphabetical order: > > library(tables) > Nodes <- c(1,10,20,2) > Values <- c(1,2,3,4) > Data <- data.frame(cbind(Nodes,Values)) > data$Nodes<- as.factor(as.character(data$Nodes)) # necessary to get factors for tabular > > tabular(Nodes ~ Values*mean, data=data) > > Values > Nodes mean > 1 1 > 10 2 > 2 4 > 20 3 > > And what I want is this:Ypu are failing to pay attention to capitalization of "Data" and not using the levels argument to factor() as you should be: Data$Nodes<- factor(Data$Nodes, levels=unique(Nodes[order(Nodes)])) # using the numeric order rather than the lexigraphic order tabular(Nodes ~ Values*mean, data=Data)> > Values > Nodes mean > 1 1 > 2 4 > 10 2 > 20 3 > > Any idea how to do this? (the solution is not to write 01, 02, 10, 20, because I use Nodes in lot of places elsewhere, where I can't use 01, etc.) > > Cheers > > Renger >David Winsemius Alameda, CA, USA
The order in the table is the order of the levels in the factor, so the best thing to do is to set the order in the factor itself. You can do this using the factor function when you create the factor, or with a function like relevel after the function has been created. In your case, what happens if you leave out the as.character part? I think that is what is causing the problem and you would get a factor like you want with just factor(data$Nodes). On Tue, Oct 8, 2013 at 11:29 AM, Renger van Nieuwkoop < renger@vannieuwkoop.ch> wrote:> Hi > I am using the package tables and want to have the rows in the numerical > order and not in the alphabetical order: > > library(tables) > Nodes <- c(1,10,20,2) > Values <- c(1,2,3,4) > Data <- data.frame(cbind(Nodes,Values)) > data$Nodes<- as.factor(as.character(data$Nodes)) # necessary to get > factors for tabular > > tabular(Nodes ~ Values*mean, data=data) > > Values > Nodes mean > 1 1 > 10 2 > 2 4 > 20 3 > > And what I want is this: > > Values > Nodes mean > 1 1 > 2 4 > 10 2 > 20 3 > > Any idea how to do this? (the solution is not to write 01, 02, 10, 20, > because I use Nodes in lot of places elsewhere, where I can't use 01, etc.) > > Cheers > > Renger > > > > > _________________________________________ > Renger van Nieuwkoop > Centre of Economic Research (CER-ETH) > Zürichbergstrasse 18 (ZUE) > CH - 8032 Zürich > +41 44 632 02 63 > mailto: rengerv@etzh.ch > blog.modelworks.ch > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]