Creating IDs based on the data given.

Hi.
I have a data of schools with the number of students. What I want is that I need to create codes/ids for each student. For example if the school-1 has 4 students, then the student codes is S1C1, S1C2, S1C3, S1C4.
This process has to be repeated for all schools. My original data has 140 schools.
My desired output is given in data frame "student_code". Is this possible?

library(tidyverse)


school_data<-tibble::tribble(
                                  ~sch_name, ~total_students,
                              "HPS KIRESUR",              2L,
                               "GHPS KOTUR",              3L,
                               "LPS HEBSUR",              5L,
              "GHPS NEKAR NAGAR HALE HUBLI",              4L,
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",              5L
  )


#desired output
student_code<-tibble::tribble(
                                  ~sch_name, ~student_code,
                              "HPS KIRESUR",        "S1C1",
                              "HPS KIRESUR",        "S1C2",
                               "GHPS KOTUR",        "S2C1",
                               "GHPS KOTUR",        "S2C2",
                               "GHPS KOTUR",        "S2C3",
                               "LPS HEBSUR",        "S3C1",
                               "LPS HEBSUR",        "S3C2",
                               "LPS HEBSUR",        "S3C3",
                               "LPS HEBSUR",        "S3C4",
                               "LPS HEBSUR",        "S3C5",
              "GHPS NEKAR NAGAR HALE HUBLI",        "S4C1",
              "GHPS NEKAR NAGAR HALE HUBLI",        "S4C2",
              "GHPS NEKAR NAGAR HALE HUBLI",        "S4C3",
              "GHPS NEKAR NAGAR HALE HUBLI",        "S4C4",
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",        "S5C1",
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",        "S5C2",
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",        "S5C3",
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",        "S5C4",
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",        "S5C5"
  )
Created on 2022-06-30 by the reprex package (v2.0.1)

The answer to this is almost always "yes"!

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

school_data<-tibble::tribble(
  ~sch_name, ~total_students,
  "HPS KIRESUR",              2L,
  "GHPS KOTUR",              3L,
  "LPS HEBSUR",              5L,
  "GHPS NEKAR NAGAR HALE HUBLI",              4L,
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",              5L
)

school_data %>% 
  # number each school
  dplyr::mutate(school_number = row_number()) %>%
  # use "uncount" - does the opposite of dplyr::count()
  tidyr::uncount(total_students) %>% 
  # number each student in each school
  dplyr::group_by(sch_name) %>% 
  dplyr::mutate(student_number = row_number()) %>%
  # combine numbers together to get ID
  dplyr::mutate(student_code = stringr::str_c("S", school_number, "C", student_number),
                .keep = "unused")
#> # A tibble: 19 × 2
#> # Groups:   sch_name [5]
#>    sch_name                                student_code
#>    <chr>                                   <chr>       
#>  1 HPS KIRESUR                             S1C1        
#>  2 HPS KIRESUR                             S1C2        
#>  3 GHPS KOTUR                              S2C1        
#>  4 GHPS KOTUR                              S2C2        
#>  5 GHPS KOTUR                              S2C3        
#>  6 LPS HEBSUR                              S3C1        
#>  7 LPS HEBSUR                              S3C2        
#>  8 LPS HEBSUR                              S3C3        
#>  9 LPS HEBSUR                              S3C4        
#> 10 LPS HEBSUR                              S3C5        
#> 11 GHPS NEKAR NAGAR HALE HUBLI             S4C1        
#> 12 GHPS NEKAR NAGAR HALE HUBLI             S4C2        
#> 13 GHPS NEKAR NAGAR HALE HUBLI             S4C3        
#> 14 GHPS NEKAR NAGAR HALE HUBLI             S4C4        
#> 15 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S5C1        
#> 16 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S5C2        
#> 17 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S5C3        
#> 18 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S5C4        
#> 19 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S5C5

Created on 2022-06-30 by the reprex package (v2.0.1)

Wow! This was of great help. Solved the problem in seconds. Thanks a lot!

Regards,
Nithin

Moving conversating from this thread over to this post as a follow-up question.

@kuttan98, can you repost your data here, as well as what you have tried and any errors that you get?

Can I do a bit of a modification here? I actually need the student codes as S001C001, S001C002 and so on for the first school, S020C001, S020C002 and so on for the 20th school. How can I set this particular pattern here?

Like so?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

school_data<-tibble::tribble(
  ~sch_name, ~total_students,
  "HPS KIRESUR",              2L,
  "GHPS KOTUR",              3L,
  "LPS HEBSUR",              5L,
  "GHPS NEKAR NAGAR HALE HUBLI",              4L,
  "GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI",              5L
)

school_data %>% 
  # number each school
  dplyr::mutate(school_number = row_number()) %>%
  # use "uncount" - does the opposite of dplyr::count()
  tidyr::uncount(total_students) %>% 
  # number each student in each school
  dplyr::group_by(sch_name) %>% 
  dplyr::mutate(student_number = row_number(),
                across(c(student_number, school_number), ~stringr::str_pad(.x, 3, pad = "0"))) %>%
  # combine numbers together to get ID
  dplyr::mutate(student_code = stringr::str_c("S", school_number, "C", student_number),
                .keep = "unused")
#> # A tibble: 19 × 2
#> # Groups:   sch_name [5]
#>    sch_name                                student_code
#>    <chr>                                   <chr>       
#>  1 HPS KIRESUR                             S001C001    
#>  2 HPS KIRESUR                             S001C002    
#>  3 GHPS KOTUR                              S002C001    
#>  4 GHPS KOTUR                              S002C002    
#>  5 GHPS KOTUR                              S002C003    
#>  6 LPS HEBSUR                              S003C001    
#>  7 LPS HEBSUR                              S003C002    
#>  8 LPS HEBSUR                              S003C003    
#>  9 LPS HEBSUR                              S003C004    
#> 10 LPS HEBSUR                              S003C005    
#> 11 GHPS NEKAR NAGAR HALE HUBLI             S004C001    
#> 12 GHPS NEKAR NAGAR HALE HUBLI             S004C002    
#> 13 GHPS NEKAR NAGAR HALE HUBLI             S004C003    
#> 14 GHPS NEKAR NAGAR HALE HUBLI             S004C004    
#> 15 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S005C001    
#> 16 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S005C002    
#> 17 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S005C003    
#> 18 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S005C004    
#> 19 GLPS CHABBI PLOT NEKAR NAGAR HALE HUBLI S005C005

Awesome!! Thanks a lot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.