I have a big dataset that has the scores of students of about 2500 students. There are about 100 questions also. But the raw data does not have the scores, only responses. The first data below shows the responses. 1 for correct answer, 0 for incorrect answer and -99 for No Response. My end result must be the one like shown in the second set. After each question, the score must appear. When 1 is the response, the corresponding score (which can vary according to each question) must appear and if it is 0 or -99, the corresponding score 0 must appear. I want the set of codes which can help me do it. Because otherwise, doing it manually every time is difficult. Can I use looping and do something about this?
library(tidyverse)
library(janitor)
tibble::tribble(
~Student.ID, ~Gender, ~School, ~l1c1, ~l1c2, ~l1c3, ~l1c4,
"ST001", 1L, "BVB", 1L, 1L, -99L, 1L,
"ST002", 1L, "CSET", 1L, 2L, 1L, 1L,
"ST003", 1L, "BVB", 1L, 1L, 1L, 1L,
"ST004", 2L, "CVCET", 0L, 1L, 1L, 1L,
"ST005", 1L, "IIT", 0L, 1L, 1L, 0L,
"ST006", 1L, "IIM", 0L, 1L, 1L, 0L,
"ST007", 2L, "IIM", 0L, 1L, 1L, 0L,
"ST008", 2L, "IIM", 1L, 1L, 1L, 0L,
"ST009", 2L, "IIM", 1L, 2L, 0L, 1L,
"ST010", 2L, "VIT", 1L, 2L, 0L, 1L,
"ST011", 2L, "VIT", 0L, 2L, 0L, 1L,
"ST012", 2L, "VIT", 0L, -99L, 0L, 0L,
"ST013", 2L, "VIT", 1L, 0L, 0L, 0L,
"ST014", 1L, "VIT", 0L, 0L, 0L, 1L,
"ST015", 1L, "IIT", 0L, 0L, 1L, 1L,
"ST016", 1L, "IIT", 1L, 0L, 1L, 1L,
"ST017", 1L, "IIT", -99L, -99L, 1L, 1L,
"ST018", 1L, "IIT", -99L, 1L, 1L, 1L,
"ST019", 1L, "IIT", -99L, 1L, 0L, -99L,
"ST020", 1L, "IIT", 1L, 1L, 0L, 1L
)
tibble::tribble(
~Student.ID, ~Gender, ~School, ~l1c1, ~score, ~l1c2, ~score, ~l1c3, ~score, ~l1c4, ~score,
"ST001", 1L, "BVB", 1L, 2L, 1L, 2L, -99L, 0L, 1L, 1L,
"ST002", 1L, "CSET", 1L, 2L, 2L, 0L, 1L, 1L, 1L, 1L,
"ST003", 1L, "BVB", 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
"ST004", 2L, "CVCET", 0L, 0L, 1L, 2L, 1L, 1L, 1L, 1L,
"ST005", 1L, "IIT", 0L, 0L, 1L, 2L, 1L, 1L, 0L, 0L,
"ST006", 1L, "IIM", 0L, 0L, 1L, 2L, 1L, 1L, 0L, 0L,
"ST007", 2L, "IIM", 0L, 0L, 1L, 2L, 1L, 1L, 0L, 0L,
"ST008", 2L, "IIM", 1L, 2L, 1L, 2L, 1L, 1L, 0L, 0L,
"ST009", 2L, "IIM", 1L, 2L, 2L, 0L, 0L, 0L, 1L, 1L,
"ST010", 2L, "VIT", 1L, 2L, 2L, 0L, 0L, 0L, 1L, 1L,
"ST011", 2L, "VIT", 0L, 0L, 2L, 0L, 0L, 0L, 1L, 1L,
"ST012", 2L, "VIT", 0L, 0L, -99L, 0L, 0L, 0L, 0L, 0L,
"ST013", 2L, "VIT", 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L,
"ST014", 1L, "VIT", 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
"ST015", 1L, "IIT", 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
"ST016", 1L, "IIT", 1L, 2L, 0L, 0L, 1L, 1L, 1L, 1L,
"ST017", 1L, "IIT", -99L, 0L, -99L, 0L, 1L, 1L, 1L, 1L,
"ST018", 1L, "IIT", -99L, 0L, 1L, 2L, 1L, 1L, 1L, 1L,
"ST019", 1L, "IIT", -99L, 0L, 1L, 2L, 0L, 0L, -99L, 0L,
"ST020", 1L, "IIT", 1L, 2L, 1L, 2L, 0L, 0L, 1L, 1L
)