Identify ranking position



How do I identify which position a particular variable has in a list?

For example, in the table below, I would like to refer to which ranking "Town 3" has in the table (based on population). It's important that I can search by Town name.

Please help!

Town_name Population
Town1 50
Town2 25
Town3 60
Town4 34


Are you sure it's in a list? Your example looks like a it's in a data.frame.

Assuming it's in a data.frame we can do something like the following. I start with making some example data. Then order it by val then make a column called position which is the rank of the ordered data. Then we can select an item with filter

data.frame(letters, val=rnorm(26)) %>%
  arrange(desc(val)) %>%
  mutate(position=row_number()) ->

ordered_df %>% ## so you can see the sorted and labeled data:
#>   letters       val position
#> 1       q 1.2262014        1
#> 2       m 1.1905709        2
#> 3       a 1.0976198        3
#> 4       c 1.0452764        4
#> 5       n 0.8114970        5
#> 6       f 0.7921563        6

ordered_df %>%
  filter(letters=='s') ## select the row where letters==s
#>   letters         val position
#> 1       s -0.06578744       15

Does that get you where you want to go?


You are a genius. Thank you so much!


not at all... I have to google syntax every day. I just recognized your design pattern.