I have a dataset of students' assessments. The first dataframe data1 is how the data looks like. But the template that is needed is like the second dataframe data2. The score must appear to the column adjacent to the response column. The scoring will change as per the question. Here in this case, it is as follows:
l1c1_identify_pic1,l1c1_identify_pic2,l1c1_identify_pic3= 2 marks when response is 2, 1 when response is 1 and 0 for 0 and -99 (no response).
l1cq_identify_col1,l1c1_identify_col2,l1c1_cards_order= 1 mark when response is 1, 0 when response is 0 and -99 (No response).
If this process can be done with looping and functions, it could be very useful for me. I tried but couldn't figure it out.
library(tidyverse)
data1<-tibble::tribble(
~student_id, ~age, ~demo_tv, ~demo_regrigerator, ~demo_phone, ~demo_laptop, ~l1c1_identify_pic1, ~l1c1_identify_pic2, ~l1c1_identify_pic3, ~l1c1_identify_col1, ~l1c1_identify_col2, ~l1c1_identify_shape1, ~l1c1_cards_order,
"S001", 8L, 0L, 1L, 0L, 0L, 2L, 1L, 0L, 1L, 1L, 1L, 0L,
"S002", 6L, 1L, 0L, 0L, 1L, 1L, 0L, 2L, 0L, 0L, 1L, -99L,
"S003", 8L, 1L, 0L, 0L, 1L, 1L, 1L, -99L, 1L, -99L, 1L, 1L,
"S004", 8L, 1L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 0L, 1L,
"S005", 8L, 1L, 1L, 0L, 1L, 2L, 0L, 2L, 1L, 0L, 1L, 0L,
"S006", 8L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, -99L,
"S007", 7L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, -99L, 1L,
"S008", 6L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, -99L, 0L, 0L, 0L,
"S009", 6L, 1L, 1L, 1L, 1L, 2L, 1L, -99L, 0L, 0L, 1L, 1L,
"S010", 6L, 1L, 1L, 1L, 1L, 2L, 0L, 0L, 0L, 0L, 1L, 0L,
"S011", 7L, 1L, 0L, 0L, 0L, -99L, 0L, 2L, 0L, 0L, 0L, -99L,
"S012", 8L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, -99L, 0L, 1L,
"S013", 8L, 0L, 1L, 0L, 1L, 2L, -99L, 2L, 1L, 1L, 0L, 0L,
"S014", 6L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
"S015", 6L, 0L, 0L, 0L, 1L, 2L, 0L, 1L, 0L, 0L, 1L, -99L,
"S016", 6L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, -99L, 0L,
"S017", 6L, 1L, 1L, 0L, 1L, 0L, -99L, 2L, 0L, 0L, 0L, 1L,
"S018", 8L, 0L, 0L, 1L, 1L, 2L, 1L, 1L, 0L, 1L, 1L, 1L,
"S019", 6L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, -99L, 0L, 0L, -99L,
"S020", 8L, 0L, 0L, 0L, 1L, 0L, 2L, 1L, 1L, 1L, 0L, 0L,
"S021", 8L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L,
"S022", 7L, 1L, 0L, 1L, 1L, 0L, 1L, 2L, 0L, 1L, -99L, 1L,
"S023", 6L, 1L, 0L, 0L, 0L, 1L, 2L, -99L, 1L, 1L, 0L, 1L,
"S024", 6L, 0L, 1L, 0L, 1L, 0L, 2L, 2L, 1L, -99L, 0L, 1L
)
data2<-tibble::tribble(
~student_id, ~age, ~demo_tv, ~demo_regrigerator, ~demo_phone, ~demo_laptop, ~l1c1_identify_pic1, ~score, ~l1c1_identify_pic2, ~score, ~l1c1_identify_pic3, ~score, ~l1c1_identify_col1, ~score, ~l1c1_identify_col2, ~score, ~l1c1_identify_shape1, ~score, ~l1c1_cards_order, ~score,
"S001", 8L, 0L, 1L, 0L, 0L, 2L, 2L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,
"S002", 6L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, -99L, 0L,
"S003", 8L, 1L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