Re: [R] don't want xtab sorting "numeric" factors...

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Tue 07 Jun 2005 - 12:07:17 EST

On Mon, 2005-06-06 at 16:54 -0700, Jeff D. Hamann wrote:
> r-gurus,
>
> I couldn't find an answer to this and after an hour or so of trying all
> types or ways to do this, I've given up for now.
>
> I'm having trouble getting the results from xtabs to generate
> "unsorted" factors. I've generated a sample data.frame I want to
> create a table from, using xtabs, and the results are presented below,
>
> > temp
> treatment itpa qmd tht
> 1 0 100 7.287263 3.362501
> 2 25 100 10.461070 4.118217
> 3 50 100 16.671731 5.814391
> 4 75 100 24.033238 8.264608
> 5 100 100 27.554497 9.586341
> 6 0 200 7.032527 3.355118
> 7 25 200 9.632828 4.039846
> 8 50 200 14.821584 5.659913
> 9 75 200 20.470692 7.791525
> 10 100 200 22.977827 8.901247
> 11 0 300 6.839360 3.355788
> 12 25 300 9.437649 4.070102
> 13 50 300 13.609004 5.545991
> 14 75 300 18.159601 7.387135
> 15 100 300 20.194335 8.380818
> 16 0 600 6.232509 3.330317
> 17 25 600 8.371084 3.990557
> 18 50 600 11.512385 5.235263
> 19 75 600 14.463214 6.605290
> 20 100 600 16.030137 7.467160
> 21 0 900 5.938640 3.338342
> 22 25 900 7.610695 3.891203
> 23 50 900 10.209530 4.917330
> 24 75 900 12.528651 6.157436
> 25 100 900 13.708622 6.791764
> 26 0 1200 5.841058 3.389716
> 27 25 1200 7.174748 3.859241
> 28 50 1200 9.238635 4.689011
> 29 75 1200 11.424713 5.688340
> 30 100 1200 12.349254 6.275864
> > t <- xtabs( temp$tht ~ temp$itpa + temp$treatment )
>
> t <- xtabs( temp$tht ~ temp$itpa + temp$treatment )
> >
> > t
> temp$treatment
> temp$itpa 0 100 25 50 75
> 100 3.362501 9.586341 4.118217 5.814391 8.264608
> 1200 3.389716 6.275864 3.859241 4.689011 5.688340
> 200 3.355118 8.901247 4.039846 5.659913 7.791525
> 300 3.355788 8.380818 4.070102 5.545991 7.387135
> 600 3.330317 7.467160 3.990557 5.235263 6.605290
> 900 3.338342 6.791764 3.891203 4.917330 6.157436
> >
>
> the factors represent real values and shouldn't be sorted as
> strings. The table should read,
>
> > t
> temp$treatment
> temp$itpa 0 25 50 75 100
> 100 3.362501 4.118217 5.814391 8.264608 9.586341
> 200 3.355118 4.039846 5.659913 7.791525 8.901247
> 300 3.355788 4.070102 5.545991 7.387135 8.380818
> 600 3.330317 3.990557 5.235263 6.605290 7.467160
> 900 3.338342 3.891203 4.917330 6.157436 6.791764
> 1200 3.389716 3.859241 4.689011 5.688340 6.275864
> >
>
> this wouldn't be that big of an issue if I only did this once for the
> results, but I'm using this in Sweave and need create the tables after
> thousands of iterations.
>
> Any help would be greatly appreciated.
>
> Thanks,
> Jeff.

Jeff,

Is 'itpa' supposed to be a factor?

When using your data above, read into R using read.table, treatment and itpa are integers and the others are doubles:

> str(temp)

`data.frame':   30 obs. of  4 variables:
 $ treatment: int  0 25 50 75 100 0 25 50 75 100 ...
 $ itpa     : int  100 100 100 100 100 200 200 200 200 200 ...
 $ qmd      : num   7.29 10.46 16.67 24.03 27.55 ...
 $ tht      : num  3.36 4.12 5.81 8.26 9.59 ...


In that case, I get:

> t <- xtabs(temp$tht ~ temp$itpa + temp$treatment)
> t

         temp$treatment
temp$itpa        0       25       50       75      100
     100  3.362501 4.118217 5.814391 8.264608 9.586341
     200  3.355118 4.039846 5.659913 7.791525 8.901247
     300  3.355788 4.070102 5.545991 7.387135 8.380818
     600  3.330317 3.990557 5.235263 6.605290 7.467160
     900  3.338342 3.891203 4.917330 6.157436 6.791764
     1200 3.389716 3.859241 4.689011 5.688340 6.275864


If I convert itpa to a factor with levels as you seem to have, I get:

> temp$itpa <- factor(as.character(temp$itpa))
> temp$itpa

 [1] 100  100  100  100  100  200  200  200  200  200  300  300  300
[14] 300  300  600  600  600  600  600  900  900  900  900  900  1200
[27] 1200 1200 1200 1200

Levels: 100 1200 200 300 600 900

> temp$itpa <- factor(as.character(temp$itpa))
> t <- xtabs( temp$tht ~ temp$itpa + temp$treatment)
> t

         temp$treatment
temp$itpa        0       25       50       75      100
     100  3.362501 4.118217 5.814391 8.264608 9.586341
     1200 3.389716 3.859241 4.689011 5.688340 6.275864
     200  3.355118 4.039846 5.659913 7.791525 8.901247
     300  3.355788 4.070102 5.545991 7.387135 8.380818
     600  3.330317 3.990557 5.235263 6.605290 7.467160
     900  3.338342 3.891203 4.917330 6.157436 6.791764


So it would seem that you either need to change itpa back to a numeric value in the data frame:

> temp$itpa <- as.numeric(as.character(temp$itpa))
> t <- xtabs(temp$tht ~ temp$itpa + temp$treatment)
> t

         temp$treatment
temp$itpa        0       25       50       75      100
     100  3.362501 4.118217 5.814391 8.264608 9.586341
     200  3.355118 4.039846 5.659913 7.791525 8.901247
     300  3.355788 4.070102 5.545991 7.387135 8.380818
     600  3.330317 3.990557 5.235263 6.605290 7.467160
     900  3.338342 3.891203 4.917330 6.157436 6.791764
     1200 3.389716 3.859241 4.689011 5.688340 6.275864


OR

If you need itpa to be a factor, modify the factor levels, since they are now ordered by the character values, not the numeric values:

> temp$itpa <- factor(temp$itpa, levels = c(100, 200, 300, 600, 900,

                                            1200))

> temp$itpa
 [1] 100  100  100  100  100  200  200  200  200  200  300  300  300
[14] 300  300  600  600  600  600  600  900  900  900  900  900  1200
[27] 1200 1200 1200 1200

Levels: 100 200 300 600 900 1200

> t <- xtabs(temp$tht ~ temp$itpa + temp$treatment)
> t

         temp$treatment
temp$itpa        0       25       50       75      100
     100  3.362501 4.118217 5.814391 8.264608 9.586341
     200  3.355118 4.039846 5.659913 7.791525 8.901247
     300  3.355788 4.070102 5.545991 7.387135 8.380818
     600  3.330317 3.990557 5.235263 6.605290 7.467160
     900  3.338342 3.891203 4.917330 6.157436 6.791764
     1200 3.389716 3.859241 4.689011 5.688340 6.275864



HTH, Marc Schwartz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 07 12:13:20 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:25 EST