I have a survey data where the scoring columns have been created next to the response column. But there is a column for the total score (here in the below data it is l1c1_total). But after I create the scoring columns the position of this column changes and it comes to the front of the data. Can I do something so that the l1c_total is does not come to the front?
library(tidyverse)
data1<-tibble::tribble(
~Student_ID, ~l1c1_identify_pic1, ~l1c1_identify_pic2, ~l1c1_identify_pic3, ~l1c1_identify_pic4, ~l1c1_total,
"S001", 1L, 1L, 1L, 1L, 6L,
"S002", 1L, 0L, 1L, 1L, 4L,
"S003", 1L, 0L, 1L, 0L, 6L,
"S004", 1L, 0L, 1L, 0L, 4L,
"S005", 0L, 1L, 1L, 0L, 2L,
"S006", 0L, 0L, 1L, 0L, 4L,
"S007", 1L, 1L, 1L, 0L, 6L,
"S008", 0L, 1L, 1L, 0L, 6L,
"S009", 1L, 1L, 0L, -99L, 4L,
"S010", 0L, 0L, -99L, 0L, 4L,
"S011", 1L, 0L, 99L, 0L, 4L,
"S012", 0L, 1L, -99L, 0L, 6L,
"S013", 0L, 0L, 1L, 0L, 6L,
"S014", 0L, 0L, 1L, 0L, 6L,
"S015", 1L, 1L, 1L, 1L, 6L
)
#Greate groups of columns with different scoring rules
scoring1 = c("l1c1_identify_pic1","l1c1_identify_pic2")
scoring2 = c("l1c1_identify_pic3","l1c1_identify_pic4")
#Create the scoring columns
scoringCols = c(scoring1, scoring2)
scores = data1 %>% select(all_of(scoringCols)) %>%
#Set the rules for each scoring group
mutate(across(all_of(scoring1), function(x){ifelse(x==1, 2, 0)}),
across(all_of(scoring2), function(x){ifelse(x == 1, 1, 0)}))
#Edit the new col names
colnames(scores) = paste(colnames(scores), "score", sep = "_")
#Bind the data together and sort the scoring columns next to the original
data2 = bind_cols(data1, scores)
data2 = data2 %>%
select(-all_of(c(scoringCols, colnames(scores))), sort(colnames(data2)))
data2
#> # A tibble: 15 x 10
#> Student_ID l1c1_total l1c1_identify_pic1 l1c1_identify_pic1~ l1c1_identify_p~
#> <chr> <int> <int> <dbl> <int>
#> 1 S001 6 1 2 1
#> 2 S002 4 1 2 0
#> 3 S003 6 1 2 0
#> 4 S004 4 1 2 0
#> 5 S005 2 0 0 1
#> 6 S006 4 0 0 0
#> 7 S007 6 1 2 1
#> 8 S008 6 0 0 1
#> 9 S009 4 1 2 1
#> 10 S010 4 0 0 0
#> 11 S011 4 1 2 0
#> 12 S012 6 0 0 1
#> 13 S013 6 0 0 0
#> 14 S014 6 0 0 0
#> 15 S015 6 1 2 1
#> # ... with 5 more variables: l1c1_identify_pic2_score <dbl>,
#> # l1c1_identify_pic3 <int>, l1c1_identify_pic3_score <dbl>,
#> # l1c1_identify_pic4 <int>, l1c1_identify_pic4_score <dbl>
Created on 2022-03-24 by the reprex package (v2.0.1)