Sort column alphabetically and then create a new column from 1

Viewed 56

I have a dataframe below:

Data<- c("Chelsea", "Arsenal", "Liverpool", "Brighton", "Fulham", "Tottenham", "Newcastle", "Mancity", "Southampton", "Wolverhampton",
"Chelsea", "Arsenal", "Liverpool", "Brighton", "Fulham", "Tottenham", "Mancity", "Chelsea", "Arsenal", "Mancity", "Liverpool", "Brighton", "Mancity",
 "Arsenal", "Liverpool", "Mancity", "Chelsea")

I want to order the column alphabetically and then create a new group starting from 1, so the output will look like this: Desired output

4 Answers

With sort and as.factor + as.numeric:

data.frame(Club = sort(Data),
           Group = as.numeric(as.factor(sort(Data))))

or with transform:

transform(data.frame(Club = sort(Data)), 
          Group = as.numeric(factor(Club)))

            Club Group
1        Arsenal     1
2        Arsenal     1
3        Arsenal     1
4        Arsenal     1
5       Brighton     2
6       Brighton     2
7       Brighton     2
8        Chelsea     3
9        Chelsea     3
10       Chelsea     3
11       Chelsea     3
12        Fulham     4
13        Fulham     4
14     Liverpool     5
15     Liverpool     5
16     Liverpool     5
17     Liverpool     5
18       Mancity     6
19       Mancity     6
20       Mancity     6
21       Mancity     6
22       Mancity     6
23     Newcastle     7
24   Southampton     8
25     Tottenham     9
26     Tottenham     9
27 Wolverhampton    10

We could use rleid:

library(tibble)
library(dplyr)
library(data.table)

tibble(Club = sort(Data)) %>% 
  mutate(Group = rleid(Club))
 Club     Group
   <chr>     <int>
 1 Arsenal       1
 2 Arsenal       1
 3 Arsenal       1
 4 Arsenal       1
 5 Brighton      2
 6 Brighton      2
 7 Brighton      2
 8 Chelsea       3
 9 Chelsea       3
10 Chelsea       3
# ... with 17 more rows
  • We can use
library(dplyr)

data.frame(Data) |> arrange(Data) |>
           group_by(g = sub("(\\w).*" , "\\1" , Data)) |>
           mutate(Group = cur_group_id()) |> select(-g)
  • Output
# A tibble: 27 × 3
# Groups:   g [10]
   g     Data     Group
   <chr> <chr>    <int>
 1 A     Arsenal      1
 2 A     Arsenal      1
 3 A     Arsenal      1
 4 A     Arsenal      1
 5 B     Brighton     2
 6 B     Brighton     2
 7 B     Brighton     2
 8 C     Chelsea      3
 9 C     Chelsea      3
10 C     Chelsea      3
# … with 17 more rows
library(data.table)

Data <- c("Chelsea", "Arsenal", "Liverpool", "Brighton", "Fulham", "Tottenham", "Newcastle", "Mancity", "Southampton", "Wolverhampton",
"Chelsea", "Arsenal", "Liverpool", "Brighton", "Fulham", "Tottenham", "Mancity", "Chelsea", "Arsenal", "Mancity", "Liverpool", "Brighton", "Mancity",
 "Arsenal", "Liverpool", "Mancity", "Chelsea")

dt <- data.table(Data)
dt[, Group := .GRP, keyby = Data]

dt
#>              Data Group
#>  1:       Arsenal     1
#>  2:       Arsenal     1
#>  3:       Arsenal     1
#>  4:       Arsenal     1
#>  5:      Brighton     2
#>  6:      Brighton     2
#>  7:      Brighton     2
#>  8:       Chelsea     3
#>  9:       Chelsea     3
#> 10:       Chelsea     3
#> 11:       Chelsea     3
#> 12:        Fulham     4
#> 13:        Fulham     4
#> 14:     Liverpool     5
#> 15:     Liverpool     5
#> 16:     Liverpool     5
#> 17:     Liverpool     5
#> 18:       Mancity     6
#> 19:       Mancity     6
#> 20:       Mancity     6
#> 21:       Mancity     6
#> 22:       Mancity     6
#> 23:     Newcastle     7
#> 24:   Southampton     8
#> 25:     Tottenham     9
#> 26:     Tottenham     9
#> 27: Wolverhampton    10
#>              Data Group

Created on 2022-08-22 by the reprex package (v2.0.1.9000)

Related