[R] sorting data whilst ignoring NA's

Peter Ehlers ehlers at ucalgary.ca
Tue Mar 9 19:46:33 CET 2010


On 2010-03-09 9:57, emwater wrote:
>
> Hello,
>
> I am very new to R and have hit my first main problem that I hope someone
> can easily resolve.
>
> I have some data that looks like this (there are 20,000 rows):
>
>> qdata
>          day month year    flow
>     [1,]   2    10 1945      NA
>     [2,]   3    10 1945      NA
>     [3,]   4    10 1945      NA
>     [4,]   5    10 1945      NA
>     [5,]   6    10 1945   2.973
>     [6,]   7    10 1945      NA
>     [7,]   8    10 1945      NA
>     [8,]   9    10 1945   2.973
>     [9,]  10    10 1945      NA
>    [10,]  11    10 1945      NA
>    [11,]  12    10 1945      NA
>    [12,]  13    10 1945      NA
>    [13,]  14    10 1945   2.973
>    [14,]  15    10 1945      NA
>
>
> I want to sort the data in descending order by flow but with all the NA
> results sent to the bottom. I have managed to sort the data using:
>
> topflows<- qdata[order(-flow, na.last=TRUE),]

Am I missing something? Or do you just want:

  qdata[order(qdata$flow, decreasing=TRUE), ]

  -Peter Ehlers

>
> But this sorts the data by flow value and then year I think giving:
>
>   day month year   flow
>   [1,]  14     3 1947 222.40
>   [2,]  15     3 1947     NA
>   [3,]  18     3 1947     NA
>   [4,]  17     3 1947     NA
>   [5,]  19     3 1947     NA
>   [6,]  12     3 1947     NA
>   [7,]  13     3 1947     NA
>   [8,]  16     3 1947     NA
>   [9,]  20     3 1947     NA
> [10,]  30    11 1946 106.50
> [11,]  30     3 1947 105.60
> [12,]  29    11 1946     NA
> [13,]  11     3 1947     NA
> [14,]  22    11 1946  99.96
> [15,]  21    11 1946     NA
>
> I want my data to come out like this:
> [1,] 14  3  1947  222.3
> [2,] 30  11 1946 106.50
> [3,] 30  3  1947  105.60
> [4,] 22 11 1946  99.96 and so on...
>
>
> Any help would be greatly valued.
>
> Thanks
>
> Emma
>
>
>

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list