Reputation: 1
If I have a vector a = c(1300,NA,NA,NA,NA,1500,NA,NA,6000,NA,NA,900)
How can I order this vector to result in:
b=[2,NA,NA,NA,NA,3,NA,NA,4,NA,NA,5]
?
Sidenote :I tried to make them repeat so it was
a=[1300,1300,1300,1300,1300,1500,1500,1500,6000,6000,6000,900]
But when I use rank its gets some crazy half numbers, any ideas? I'm at wits end for figuring this out.
Keeping the amount of NAs after a number is very important here so I cant just only ignore them
Upvotes: 0
Views: 190
Reputation: 99331
Take a ^ is.na(a)
and multiply it by rank(a)
. We use ties="first"
to ensure we get increasing values at each index, not averages.
rank(a, ties="first") * a ^ is.na(a)
# [1] 2 NA NA NA NA 3 NA NA 4 NA NA 1
Upvotes: 1
Reputation: 32548
replace(a, !is.na(a), rank(a[!is.na(a)], ties.method = "first"))
# [1] 2 NA NA NA NA 3 NA NA 4 NA NA 1
Upvotes: 1
Reputation: 145775
The dplyr::dense_rank
function behaves as you want:
library(dplyr)
dense_rank(a)
# [1] 2 NA NA NA NA 3 NA NA 4 NA NA 1
It also works on the dense vector:
b = c(1300,1300,1300,1300,1300,1500,1500,1500,6000,6000,6000,900)
dense_rank(b)
# [1] 2 2 2 2 2 3 3 3 4 4 4 1
Upvotes: 1