Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ORDER only returning first row

Tags:

r

I have a fairly simple data.frame (fract_data) that I'm trying to sort and yet when I apply the ORDER function only the first row is returned. Shouldn't all the rows be returned? How should I be using ORDER to return all the rows?

    fract_data[order("date"),]
        date code rank.ey rank.roce rank.combined rank.final fract
1 2005-01-31  ABC       1         8             9          4     3

As shown below there are 48 rows in fract_data

attributes(fract_data)
$names
[1] "date"          "code"          "rank.ey"       "rank.roce"     "rank.combined" "rank.final"    "fract"        

$class
[1] "data.frame"

$row.names
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
[42] 42 43 44 45 46 47 48

It also doesn't appear to be a print or display issue given the attributes returned below where fract_data2 has only 1 row.

> fract_data2 <- fract_data[order("date"),]
> attributes(fract_data2)
$names
[1] "date"          "code"          "rank.ey"       "rank.roce"     "rank.combined" "rank.final"    "fract"        

$row.names
[1] 1

$class
[1] "data.frame"
like image 621
getting-there Avatar asked Dec 13 '22 05:12

getting-there


1 Answers

order() expects one or more vectors, and you feed it a character vector containing only one element, "date". Try fract_data[order(fract_data$date), ] instead.

like image 94
baptiste Avatar answered Jan 04 '23 14:01

baptiste