Hi. I have a dataset of students with the details of teachers (like their education and experience). The raw data is like the one I show below in data1. The data of teachers lie in different columns. I want the data in the desired format as shown in the data2.
How can I do this?
library(tidyverse)
data1<-tibble::tribble(
~student_id, ~tch1_degree, ~tch1_exp, ~tch2_degree, ~tch2_exp, ~tch3_degree, ~tch3_exp,
"S001", 1L, 14L, NA, NA, NA, NA,
"S002", NA, NA, 2L, 22L, NA, NA,
"S003", NA, NA, NA, NA, 3L, 16L,
"S004", NA, NA, NA, NA, 2L, 14L,
"S005", NA, NA, 2L, 24L, NA, NA,
"S006", NA, NA, 1L, 22L, NA, NA,
"S007", NA, NA, 1L, 20L, NA, NA,
"S008", 2L, 10L, NA, NA, NA, NA,
"S009", 2L, 10L, NA, NA, NA, NA
)
data2<-tibble::tribble(
~student_id, ~tch_degree, ~tch_exp,
"S001", 1L, 14L,
"S002", 2L, 22L,
"S003", 3L, 16L,
"S004", 2L, 14L,
"S005", 2L, 24L,
"S006", 1L, 22L,
"S007", 1L, 20L,
"S008", 2L, 10L,
"S009", 2L, 10L
)