-2
testdf = data.frame(name=c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"), score = c(11,15,46,32,22,68,89,35,54,12,44,3,1,88,6))
testdf$rank<-rank(testdf$score)

when using the rank function in R it doesnt include leading zeros-- it ranks

1 
2
3
...
10
11
12

but that means sometimes when it sorts it sorts as: 1, 10, 11, 12...2, 21, 22, 23...3, 31, 32, 33. instead of 1, 2, 3, 4.

is there a way to include the leading zero with the rank function such that the output is 01, 02, 03, 04, etc

the complete example that i am using is not reproducible on here. i am using the rank function then combining it with another column to create a rank and total (converts to character). See this image of the output

https://i.stack.imgur.com/V8PzC.jpg

you will see that it is ordering by 1, 10, 11...2, 20, 21, etc. rather than 1,2,3,4...which is why i was hoping to include leading zeros because when i combine the columns it is no longer a numeric vector

Jaap
  • 81,064
  • 34
  • 182
  • 193
Tyler
  • 93
  • 1
  • 9
  • 1
    Can you include a [mcve]? `rank` should return `"A numeric vector"` which you can sort in R using proper numeric sorting. How are you trying to sort this? Are you converting the output of `rank` into `character` and then trying to sort? Please show us a complete example of what you're doing, what is happening, and what you want to happen. – divibisan Apr 22 '19 at 22:47
  • adding now...the data is included – Tyler Apr 22 '19 at 22:54
  • when I sort on the `score` column I get an ascending `rank` column. Could you provide your output and why it doesn't match your expectations? – D.sen Apr 22 '19 at 22:58
  • It will sort it as `1, 10, 11, 12...` only when they are characters. If they are kept as numeric it would sort it as expected. – Ronak Shah Apr 22 '19 at 22:59
  • yes i understand that it will order like that, which is why i was asking if there is a way within the function to include leading zeros. the complete example that i am using is no reproducible on here. i am using the rank function then combining it with another column to create a rank and total. See this image of the output https://imgur.com/a/jKfIFWs you will see that it is ordering by 1, 10, 11...2, 20, 21, etc. rather than 1,2,3,4...which is why i was hoping to include leading zeros because when i combine the columns it is no longer a numeric vector – Tyler Apr 22 '19 at 23:03
  • There is also `gtools::mixedorder` and `gtools::mixedsort` which might help you in this case. – Ronak Shah Apr 22 '19 at 23:14
  • Thanks Ronak, but this doesnt answer the question. I saw you marked this as duplicate but it isnt a duplicate...I am asking if there is the ability within the Rank function to include leading zeros (e.g., add_zero = TRUE, or something similar) – Tyler Apr 22 '19 at 23:28
  • If you read `?rank` there is no such provision in the `rank` function. To get your expected output you need to hack around with `sprintf` or similar other functions as shown in the duplicate. (You also have got similar answers here). Hence, I marked it as duplicate of that question. – Ronak Shah Apr 23 '19 at 00:20
  • I did read ?rank and that was why I was asking about it...because I didn't see it in there. I have run into help files with missing data, or I could miss it myself even if it's staring me in the face sometimes. – Tyler Apr 25 '19 at 22:29

2 Answers2

0

What you're looking to do is pad the string with leading 0's. It's important to pad, rather than just add a 0 with paste or sprintf, because while 02 will come before 10, it won't come before 010. The below options will work with any vector and are not rank specific. This question (probably a duplicate) shows many options: How to add leading zeros?

Here's the easiest way:

testdf$rank
  [1]  4  6 11  8  7 13 15  9 12  5 10  2  1 14  3

stringr::str_pad(testdf$rank, width = 3, side = 'left', pad = '0')

 [1] "004" "006" "011" "008" "007" "013" "015" "009" "012" "005" "010" "002"
[13] "001" "014" "003"

The width argument tells how long you want these strings to be, and the pad argument will be added to the side side of the string until they're all the same length

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • i appreciate that...i know how to add the leading zeros with stringr, but i was wondering if there is an ability to do it within the rank function (e.g., add_zero = TRUE, or something similar) – Tyler Apr 22 '19 at 23:27
  • No, since rank returns a numeric vector and there is no concept of leading zeros in a `numeric`. It's only a thing you can or might want to do if you're converting from numeric to character – divibisan Apr 22 '19 at 23:29
0

Try this (replace 2 in %02d with the number of digits that should be there for any rank):

testdf$rank <- sprintf("%02d",rank(testdf$score))
jsv
  • 740
  • 3
  • 5