Hi There, I thought I might add to this chain, because my problem is similar.
I was successful at recoding my Likert variable from character to numeric, using a similar method as written above.
But after that I've been trying to use sapply in order to change all my variables without copy-pasting my command. I achieved this by creating a function myrecode (see below). It WORKS! But Im having a problem: how can I keep the information of the Day and Study Arm that are not in the recoded command in conjunction with my newly recoded variables?
#first I created the recode function I want to apply to my dataset
myrecode <- function(x){
recode(x, "Not at all"=1, "A little"=2, "Moderately"=3, "Quite a bit" =4, "Extremely" = 5)
}
#then I created an index for the 22 columns I will want to convert
ind <- my_data[,4:26]
#then I applied it to the variable columns
my_data_2 <-
sapply(ind, myrecode)
MY PROBLEM NOW IS THAT I DON'T HAVE THE RECODED VALUES ALONG WITH MY STUDY ARM AND DAY VARIABLE Hope this makes sense. Thanks so much in advance.
here's a portion of my data frame which is called my_data to make this reproducible
structure(list(Day = c("0", "1", "2", "3", "4", "5", "6", "7",
"9", "10"), `Study Arm` = c("B", "B", "B", "B", "B", "B", "B",
"B", "B", "B"), `Low energy` = c("Not at all", "Not at all",
"Not at all", "Not at all", "Not at all", "Not at all", "Not at all",
"Not at all", "Not at all", "Not at all"), Yawning = c("Not at all",
"Not at all", "Not at all", "Not at all", "Not at all", "Not at all",
"A little", "Not at all", "Not at all", "Not at all"), Alert = c("Extremely",
"Extremely", "Extremely", "Quite a bit", "Extremely", "Extremely",
"Quite a bit", "Extremely", "Extremely", "Extremely"), Tired = c("Not at all",
"Not at all", "Not at all", "Not at all", "Not at all", "A little",
"A little", "Not at all", "Not at all", "Not at all")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))