[R] Order with scientific notation

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 26 08:03:14 CEST 2004


Your column of P.values is not numeric.  If it were, it would be left
justified and not printed with trailing zeroes suppressed. As in

> (X <- data.frame(P.values=c(0.0379, 0.068, 0.0025, 8e-04)))
  P.values
1   0.0379
2   0.0680
3   0.0025
4   0.0008

So I guess it is factor or character, as in

> X$P.values <- as.character(X$P.values)
> X
  P.values
1   0.0379
2    0.068
3   0.0025
4    8e-04

> sort(X$P.values)
[1] "0.0025" "0.0379" "0.068"  "8e-04"

str() is usually your friend in problems like this.


On Thu, 26 Aug 2004, Kevin Wang wrote:

> Hi,
> 
> If a value like 8e-04 is in a data frame, is the following behaviour normal?
> 
>    > final.df
>       Chr P.values
>    1    1   0.0379
>    2    6    0.068
>    3    2   0.0025
>    4   13    8e-04
>    5   14   0.0244
>    6    3   0.0279
>    7    4   0.1561
>    8    5   0.9261
>    9    7   0.0011
>    10   9   0.5125
>    11  10   0.2196
>    12  11   0.6457
>    13  18   0.9272
> 
>    > final.sorted <- data.frame(final.df[order(final.df$P.values), ],
>    +                            row.names = 1:13)
>    > final.sorted
>       Chr P.values
>    1    7   0.0011
>    2    2   0.0025
>    3   14   0.0244
>    4    3   0.0279
>    5    1   0.0379
>    6    6    0.068
>    7    4   0.1561
>    8   10   0.2196
>    9    9   0.5125
>    10  11   0.6457
>    11   5   0.9261
>    12  18   0.9272
>    13  13    8e-04
> 
> I'd think 8e-04 should be the smallest value...?  It's R 1.9.1 on 
> Windows XP if that helps.
> 
> Cheers,
> 
> Kevin
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list