-2

Assume my data (df) looks something like this:

Rank    Student    Points    Type
3       Liz        60        Junior
1       Sarah      100       Junior
10      John       40        Senior
2       Robert     70        Freshman
13      Jackie     33        Freshman
11      Stevie     35        Senior

I want to sort the data according to the Points, followed by Rank column in descending and ascending order, respectively, so that it looks like this:

Rank    Student    Points    Type
1       Sarah      100       Junior
2       Robert     70        Freshman
3       Liz        60        Junior
10      John       40        Senior
11      Stevie     35        Senior
13      Jackie     33        Freshman

So I did this:

df[order(df[, "Points"], df[, "Rank"]), ]

Resulted in this:

Rank    Student    Points    Type
1       Sarah      100       Junior
10      John       40        Senior
11      Stevie     35        Senior
13      Jackie     33        Freshman
2       Robert     70        Freshman
3       Liz        60        Junior

Question: How do I fix this?

I'm trying to use the column headers because the column length/width may change which can affect my sorting if I use physical locations.

FYI: I've tried so many suggestions and none seems to work: one, two, three and four...

Community
  • 1
  • 1
yastro
  • 5
  • 3
  • 4
    You have `Rank` as a string, try converting it into numeric: `df$Rank <- as.numeric(df$Rank)` (same for `Points` column) and repeat the sort again. Should work. – m0nhawk Nov 24 '15 at 20:53
  • Please add output of `str(df)` to your post. – zx8754 Nov 24 '15 at 20:56
  • Also, you may want to try the `arrange` function in `dplyr`: `arrange(df, desc(Points), Rank)` – Hao Nov 24 '15 at 20:57

3 Answers3

0

Try this:

df[order(df$Points,decreasing=T,df$Rank),]



 Rank Student Points     Type
2    1   Sarah    100   Junior
4    2  Robert     70 Freshman
1    3     Liz     60   Junior
3   10    John     40   Senior
6   11  Stevie     35   Senior
5   13  Jackie     33 Freshman
Shenglin Chen
  • 4,504
  • 11
  • 11
  • not sure what's going on but this suggestion doesn't work either. i think there's something wrong w/ how my data is stored `df <- df[order(df$Points,decreasing=TRUE, df$Rank),]` ... same result, literally – yastro Nov 24 '15 at 21:28
0

going back to the basics :) http://www.statmethods.net/management/sorting.html

so, your code should be: df <- df[order(-Points, Rank),]

nick
  • 1,090
  • 1
  • 11
  • 24
0

Like m0nhawk pointed out in his comment, you probably have the data as strings. String characters are ordered one at a time.

You need to convert them to numeric first. Also, for decreasing order you need the argument decreasing = TRUE.

df[, "Rank"] <- as.numeric(df[, "Rank"])
df[, "Points"] <- as.numeric(df[, "Points"])
df[order(df[, "Points"], decreasing = TRUE, df[, "Rank"]), ]

If the data type is 'factor' this will not work, though. You can try the following:

df <- as.data.frame(df, stringsAsFactors = FALSE)

And then the three lines above will work.

Luis
  • 481
  • 5
  • 7
  • first my data is of type "factor" but I really suspect there's something wrong with the data itself. using the your suggestion trancates the numbers in the "Points" section ... e.g. 300 points becomes 30 or some weird stuff -- plus doesn't sort the "Rank" column either. data from hell? – yastro Nov 24 '15 at 22:19
  • You will definitely want to avoid having the data as factors, then. About the weird formatting, it's hard to know what is going on without seeing the data. Sharing the output of `str(df)`, as suggested by zx8754, could help. It would also be helpful to know more about how you loaded the data into R. – Luis Nov 25 '15 at 18:56
  • Luis, @m0nhawk and @-Shenglin Chen:I'm dealing with some sensitive info and so cannot even post the str(df) out. I'm happy to share some snippet offline. how can i ping or IM you? – yastro Dec 02 '15 at 22:27
  • Ideally, you should post here an example like the one you provided, but that is fully representative of your real data. One that reproduces the problem you see on your real data when you apply the solutions suggested here. The post should also include the code you used to produce the data frame, which has the double advantage of making it easier for us to work on it and ensuring reproducibility of all properties of your data. If you cannot do that, I will be happy to provide some help offline. – Luis Dec 04 '15 at 16:13
  • I'm having to take it offline where I'll be able to provide more info. How do I contact you offline? – yastro Dec 10 '15 at 20:49
  • [Email](http://www.google.com/recaptcha/mailhide/d?k=01h1j_xgyB90IVGIZ9dA1Jfw==&c=Xhj_bgIAV8FLfilx0RETrFW9ECDjyd_22S11MmM8YEI=) me. – Luis Dec 12 '15 at 04:31