s-news
[Top] [All Lists]

sorting a data frame

To: s-news@lists.biostat.wustl.edu
Subject: sorting a data frame
From: "Data Analytics Corp." <dataanalytics@earthlink.net>
Date: Tue, 29 Jan 2008 09:56:34 -0500
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=dk20050327; d=earthlink.net; b=iGknP/XyoDKwcusISf7feOk3GvtnGOmvDP5dKyCcL+AgEfbf/m1ynEpeAZHoSa9b; h=Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding:X-ELNK-Trace:X-Originating-IP;
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)
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

<Prev in Thread] Current Thread [Next in Thread>