I have 3 datasets with me in different files. These are mainly on the academic scores of students of different grades. For our research purposes, we need to merge these files vertically. The similar questions across the 3 levels will appear vertically one above the other. For example, the l1c1_score, l2c1_score and l3c1_score will be grouped together and then first grade students will appear first, then the second and the third grade will appear last. To again make it clear, the merger is vertical. The challenge I am facing is due to the difference in variable names and the number of columns is different for different levels.
library(tidyverse)
data_lvl1<-tibble::tribble(
~student_id, ~l1c1_score, ~l1c2_score, ~l1c3_score, ~l1c4_score, ~cog_total_l1,
"S001", 2L, 2L, 2L, 1L, 7L,
"S002", 2L, 1L, 2L, 2L, 7L,
"S003", 4L, 1L, 2L, 3L, 10L,
"S004", 6L, 1L, 1L, 2L, 10L,
"S005", 2L, 1L, 1L, 3L, 7L,
"S006", 2L, 2L, 1L, 3L, 8L,
"S007", 2L, 2L, 1L, 4L, 9L,
"S008", 2L, 2L, 1L, 2L, 7L,
"S009", 2L, 2L, 2L, 2L, 8L,
"S010", 4L, 1L, 2L, 4L, 11L,
"S011", 4L, 1L, 3L, 4L, 12L,
"S012", 4L, 1L, 4L, 2L, 11L,
"S013", 4L, 1L, 4L, 2L, 11L,
"S014", 3L, 1L, 4L, 4L, 12L,
"S015", 2L, 1L, 1L, 2L, 6L
)
data_lvl2<-tibble::tribble(
~student_id, ~l2c1_score, ~l2c2_score, ~l2c3_score, ~l2c4_score, ~l2c5_score, ~cog_total_l2,
"S016", 2L, 2L, 1L, 2L, 2L, 9L,
"S017", 2L, 2L, 2L, 2L, 1L, 9L,
"S018", 2L, 2L, 2L, 2L, 1L, 9L,
"S019", 2L, 4L, 3L, 2L, 1L, 12L,
"S020", 4L, 4L, 3L, 2L, 1L, 14L,
"S021", 4L, 4L, 1L, 2L, 1L, 12L,
"S022", 4L, 2L, 1L, 2L, 2L, 11L,
"S023", 5L, 2L, 1L, 3L, 2L, 13L,
"S024", 5L, 2L, 1L, 3L, 2L, 13L,
"S025", 2L, 4L, 2L, 3L, 1L, 12L,
"S026", 5L, 4L, 3L, 4L, 2L, 18L,
"S027", 5L, 4L, 3L, 4L, 1L, 17L,
"S028", 1L, 1L, 1L, 4L, 1L, 8L,
"S029", 1L, 1L, 1L, 2L, 3L, 8L,
"S030", 1L, 1L, 1L, 2L, 3L, 8L
)
data_lvl3<-tibble::tribble(
~student_id, ~l2c1_score, ~l2c2_score, ~l2c3_score, ~l2c4_score, ~l2c5_score, ~l2c6_score, ~cog_total_l1,
"S031", 4L, 1L, 3L, 1L, 3L, 3L, 15L,
"S032", 4L, 3L, 2L, 6L, 0L, 4L, 19L,
"S033", 4L, 4L, 0L, 6L, 5L, 4L, 23L,
"S034", 4L, 1L, 0L, 6L, 4L, 2L, 17L,
"S035", 4L, 6L, 2L, 3L, 5L, 2L, 22L,
"S036", 2L, 0L, 5L, 2L, 1L, 2L, 12L,
"S037", 2L, 5L, 4L, 3L, 4L, 2L, 20L,
"S038", 2L, 2L, 6L, 3L, 3L, 2L, 18L,
"S039", 2L, 5L, 0L, 4L, 0L, 2L, 13L,
"S040", 6L, 3L, 0L, 5L, 2L, 2L, 18L,
"S041", 6L, 6L, 0L, 5L, 1L, 2L, 20L,
"S042", 6L, 0L, 6L, 4L, 3L, 2L, 21L,
"S043", 6L, 6L, 3L, 4L, 2L, 2L, 23L,
"S044", 2L, 0L, 6L, 5L, 1L, 4L, 18L,
"S045", 2L, 5L, 0L, 0L, 6L, 4L, 17L
)
Created on 2022-03-31 by the reprex package (v2.0.1)