Giving spacebar in generated Random IDs

Hi,
I have a dataset where I had created random IDs for students based on their schools. But I wanted a space to come in between the code. The present codes are structured as "S001C001". Actually I want a space to come before "C". Hence the code will be "S001 C001". How can I give the command for spacebar?

library(dplyr)

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")

Just add a space before the C in your string, i.e. " C"

Thanks a lot for the help..

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.