Your issue is that the columns of x are not integer. If you look at the data
frame in the object explorer and set the precision to 12 or more decimals
you will see that the sorting is indeed correct. To solve it you could do
something like this
> x1 <- data.frame(yr = round(x$yr),month = round(x$month))
> sort.col(x1,"@ALL",1:2,T)
yr month
2 2007 4
3 2007 5
4 2007 6
5 2007 7
6 2007 8
7 2007 9
8 2007 10
9 2007 11
10 2007 12
1 2008 1
Regards,
Carlos
Carlos Alzola
calzola@cox.net
(703) 242-6747
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Data Analytics
Corp.
Sent: Tuesday, January 29, 2008 9:57 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] sorting a data frame
Good morning,
I posted a question yesterday regarding sorting a data frame. The problem
is that the sort order is not correct when I use sort.col. I received many
responses, all saying basically the same thing - I must have a factor. I
actually checked this before posting the query and all was numeric. So let
me try again but be more explicit this time, because I'm very puzzled.
I have a client file that has the dates as 1.2008, 4.2007, etc. The
commands and example I used along with the as.numeric() checks are below.
As you can see, the sort is not correct. I still don't understand why. Any
hints?
> y <- c(1.2008, 4.2007, 5.2007, 6.2007, 7.2007, 8.2007, 9.2007, 10.2007,
11.2007, 12.2007) > y [1] 1.2008 4.2007 5.2007 6.2007 7.2007 8.2007
9.2007 10.2007
11.2007 12.2007
> is.numeric(y)
[1] T
> x <- data.frame(yr = (y - floor(y))*10000, month = floor(y)) > x
yr month
1 2008 1
2 2007 4
3 2007 5
4 2007 6
5 2007 7
6 2007 8
7 2007 9
8 2007 10
9 2007 11
10 2007 12
> is.numeric(x$month)
[1] T
> sort.col(x, "@ALL", 1:2)
yr month
6 2007 8
7 2007 9
8 2007 10
9 2007 11
10 2007 12
2 2007 4
3 2007 5
4 2007 6
5 2007 7
1 2008 1
>
Any help is appreciated.
Walt Paczkowski
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with the
BODY of the message: unsubscribe s-news
|