How to rank variable and create a new variable to display the order? for example, following data frame, want to sort the data by var1 and generate a new variable for the order, let ties have the same order number. Thank you!
input<-("var1 var2
1 2
3 4
1 3
2 5
2 6
4 5
6 7")
mydata<- read.table(textConnection(input),header=T)
Here are a couple approaches using functions from dplyr
depending on how you want the ranking to be numbered
library(tidyverse)
input <- ("var1 var2
1 2
3 4
1 3
2 5
2 6
4 5
6 7")
mydata <- read.table(textConnection(input), header = T)
# ties get lower number rank
mydata %>%
mutate(rank_order = min_rank(var1)) %>%
arrange(rank_order)
#> var1 var2 rank_order
#> 1 1 2 1
#> 2 1 3 1
#> 3 2 5 3
#> 4 2 6 3
#> 5 3 4 5
#> 6 4 5 6
#> 7 6 7 7
# same as above but no gaps between ranks
mydata %>%
mutate(rank_order = dense_rank(var1)) %>%
arrange(rank_order)
#> var1 var2 rank_order
#> 1 1 2 1
#> 2 1 3 1
#> 3 2 5 2
#> 4 2 6 2
#> 5 3 4 3
#> 6 4 5 4
#> 7 6 7 5
Created on 2018-11-20 by the reprex package (v0.2.1)
3 Likes
Thank you! one more question: if there are missing value in the rank variable and want to let missing value in the last, how to do that?
1 Like
Glad to help!
If there is NA
in the ranking variable, you will get NA
in the rank order column. Is that how you want NA
s to be handled?
library(tidyverse)
# with NA
mydata2 <- tribble(
~var1, ~var2,
1, 2,
3, 4,
NA, 3,
2, 5,
2, 6,
4, 5,
6, 7
)
# ties get lower number rank
mydata2 %>%
mutate(rank_order = min_rank(var1)) %>%
arrange(rank_order)
#> # A tibble: 7 x 3
#> var1 var2 rank_order
#> <dbl> <dbl> <int>
#> 1 1 2 1
#> 2 2 5 2
#> 3 2 6 2
#> 4 3 4 4
#> 5 4 5 5
#> 6 6 7 6
#> 7 NA 3 NA
Created on 2018-11-20 by the reprex package (v0.2.1)
1 Like
Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!1
1 Like
system
Closed
November 27, 2018, 7:32pm
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.