The reason for posting here is that I'd prefer a solution that uses packages from 'tidyverse', although I'm aware it may not be fully possible.
If my question is inappropriate for this forum, please disregard/delete it and, please accept my apologies for posting out of place.
The long title would've been "create cols/var 1) named as a function of specific responses in previous cols/var and 2) that contain new values as function of previous responses positions in an array (best-worst scaling application)"
I'm performing some best-worst scaling (maxdiff) analyses and I need to convert the data from the raw responses data file into a data file suitable for BWS analyses.
My responses data file is dfex and the end result should be dfbw.
Basically, the example starting datafile is self-explanatory, i.e., there are two questions q01 and q02, and the respondents indicate which of the available alternatives to these questions are "best" or "worst" ("most useful" and "least useful", here).
The final data file, suited for BWS analysis, should indicate for each question, which alternative, out of four possible per each question, was chosen. For instance, in case 1, question 1, the respondent indicated 1st alternative as the "most useful" and 3rd alternative as the "least useful".
I currently doing this via some seriously stupid manual conversions of scores but I'm certain that much more elegant solutions are available (I'm just not that advanced in R and I'm under a lot of pressure).
Could you, please, point me in the right direction (i.e., how to automate this process and make it more smoothly) and, if possible, please indicate your reasons for choosing using a certain package or particular approach versus another (e.g., a custom function versus a combination of functions in a package/s)?
# This is my starting data frame
dfex <- data.frame(stringsAsFactors=FALSE,
q01_01 = c("most useful", "least useful", "least useful", NA),
q01_02 = c(NA, "most useful", "most useful", "most useful"),
q01_03 = c("least useful", NA, NA, "least useful"),
q01_04 = c(NA, NA, NA, NA),
q02_01 = c("least useful", "least useful", NA, "least useful"),
q02_02 = c(NA, NA, NA, NA),
q02_03 = c(NA, "most useful", "most useful", "most useful"),
q02_04 = c("most useful", NA, "least useful", NA))
# which now I'm processing manually
dfex <- dfex %>%
mutate(b01_01 = case_when(q01_01 == "most useful" ~ 1)) %>%
mutate(b01_02 = case_when(q01_02 == "most useful" ~ 2)) %>%
mutate(b01_03 = case_when(q01_03 == "most useful" ~ 3)) %>%
mutate(b01_04 = case_when(q01_04 == "most useful" ~ 4)) %>%
mutate(b02_01 = case_when(q02_01 == "most useful" ~ 1)) %>%
mutate(b02_02 = case_when(q02_02 == "most useful" ~ 2)) %>%
mutate(b02_03 = case_when(q02_03 == "most useful" ~ 3)) %>%
mutate(b02_04 = case_when(q02_04 == "most useful" ~ 4)) %>%
mutate(w01_01 = case_when(q01_01 == "least useful" ~ 1)) %>%
mutate(w01_02 = case_when(q01_02 == "least useful" ~ 2)) %>%
mutate(w01_03 = case_when(q01_03 == "least useful" ~ 3)) %>%
mutate(w01_04 = case_when(q01_04 == "least useful" ~ 4)) %>%
mutate(w02_01 = case_when(q02_01 == "least useful" ~ 1)) %>%
mutate(w02_02 = case_when(q02_02 == "least useful" ~ 2)) %>%
mutate(w02_03 = case_when(q02_03 == "least useful" ~ 3)) %>%
mutate(w02_04 = case_when(q02_04 == "least useful" ~ 4))
# create (manually) cols
dfex %>%
select(c(b01_01:b01_04)) %>%
rowSums(., na.rm = T) -> dfex$B1
dfex %>%
select(c(b02_01:b02_04)) %>%
rowSums(., na.rm = T) -> dfex$B2
dfex %>%
select(c(w01_01:w01_04)) %>%
rowSums(., na.rm = T) -> dfex$W1
dfex %>%
select(c(w02_01:w02_04)) %>%
rowSums(., na.rm = T) -> dfex$W2
# getting the desired BW datafile after selecting only cols of interest
dfbw <- dfex %>% select(B1:W2)