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):> qdataday 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),] 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 -- View this message in context: http://n4.nabble.com/sorting-data-whilst-ignoring-NA-s-tp1586277p1586277.html Sent from the R help mailing list archive at Nabble.com.
On Mar 9, 2010, at 11:57 AM, 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),]Try (untested):> topflows <- qdata[order(-qdata$flow, na.last=TRUE),]> > 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 > > > > -- > View this message in context: http://n4.nabble.com/sorting-data-whilst-ignoring-NA-s-tp1586277p1586277.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
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
Thanks for these suggestions. I tried them out and I get the error message: Error in qdata$flow : $ operator is invalid for atomic vectors Can anyone offer further suggestions on how to extract the values from atomic vectors? Thanks again Emma -- View this message in context: http://n4.nabble.com/sorting-data-whilst-ignoring-NA-s-tp1586277p1587108.html Sent from the R help mailing list archive at Nabble.com.