[R] tables with row sorted numerically although factors

David Winsemius dwinsemius at comcast.net
Tue Oct 8 19:57:17 CEST 2013


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



More information about the R-help mailing list