Separation of names and IDs from variable

Hi
I am having data where the student names and the IDs are given together (separated by "-"). My desired format is the one given in "data2". The names and the IDs are to be in separate columns. An important thing is that there are NAs in the data. And my data is a big one. How can I solve this problem?


library(tidyverse)

data<-tibble::tribble(
                   ~cal_name1,                  ~cal_name2,                         ~cal_name3,               ~cal_name4,              ~cal_name5,                    ~cal_name6,
      "Anand Byahatti-9AHCMX", "Apsana. S Hubballi-R0UQTS", "Fathima Begum.R.Kawalikai-ABUFRY", "Jyothi Byahatti-7I6QJM", "Momadsad Huggi-TFN2GP", "Rajeshwari Dalayatha-AIF3A4",
  "Netra S. Kattimani-FAU4EG",     "Preeti Salagar-ALWZO7",            "Shrikant Jadav-1LAF0L",                       NA,                      NA,                            NA
  )


data2<-tibble::tribble(
                   ~cal_name1, ~student1_id,           ~cal_name2, ~student2_id,                  ~cal_name3, ~student3_id,        ~cal_name4, ~student4_id,       ~cal_name5, ~student5_id,             ~cal_name6, ~student6_id,
             "Anand Byahatti",     "9AHCMX", "Apsana. S Hubballi",     "R0UQTS", "Fathima Begum.R.Kawalikai",     "ABUFRY", "Jyothi Byahatti",     "7I6QJM", "Momadsad Huggi",     "TFN2GP", "Rajeshwari Dalayatha",     "AIF3A4",
         "Netra S. Kattimani",     "FAU4EG",     "Preeti Salagar",     "ALWZO7",            "Shrikant Jadav",     "1LAF0L",                NA,           NA,               NA,           NA,                     NA,           NA
         )

library(tidyverse)
library(glue)

data<-tibble::tribble(
  ~cal_name1,                  ~cal_name2,                         ~cal_name3,               ~cal_name4,              ~cal_name5,                    ~cal_name6,
  "Anand Byahatti-9AHCMX", "Apsana. S Hubballi-R0UQTS", "Fathima Begum.R.Kawalikai-ABUFRY", "Jyothi Byahatti-7I6QJM", "Momadsad Huggi-TFN2GP", "Rajeshwari Dalayatha-AIF3A4",
  "Netra S. Kattimani-FAU4EG",     "Preeti Salagar-ALWZO7",            "Shrikant Jadav-1LAF0L",                       NA,                      NA,                            NA
)


data2<-tibble::tribble(
  ~cal_name1, ~student1_id,           ~cal_name2, ~student2_id,                  ~cal_name3, ~student3_id,        ~cal_name4, ~student4_id,       ~cal_name5, ~student5_id,             ~cal_name6, ~student6_id,
  "Anand Byahatti",     "9AHCMX", "Apsana. S Hubballi",     "R0UQTS", "Fathima Begum.R.Kawalikai",     "ABUFRY", "Jyothi Byahatti",     "7I6QJM", "Momadsad Huggi",     "TFN2GP", "Rajeshwari Dalayatha",     "AIF3A4",
  "Netra S. Kattimani",     "FAU4EG",     "Preeti Salagar",     "ALWZO7",            "Shrikant Jadav",     "1LAF0L",                NA,           NA,               NA,           NA,                     NA,           NA
)


#solve once
does_one <- function(d,col){
d |> select(col) |> separate(col=col,
                                      into = c(col,glue("student{parse_number(col)}_id")),
                                      sep = "-")
}

#test 
data |> does_one("cal_name2")

# do multiple times

(data_solved <- map_dfc(names(data),
                         ~does_one(data,.x)))


identical(data_solved,data2)

Thanks a lot for this.

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.