I'm trying to mutate variables in a data frame that has 10 of the 55 variables in the dataset that I'm using. 5 of those 10 variables are categorical and as such, I am trying to convert them into factors (as characters). However, every time R does the mutation, it applies the mutation to those 5 factors as if I never put them into a separate data frame. R will only apply the mutation onto them in the entire group of 55 variables. I've highlighted and rerun the code as a whole and for each variable individually multiple times and it still won't work. When I use the (view) function on the ten variables I want and hover each of the variable names in the table displayed, the mutations go away for some reason, as if I didn't do them. When I use (view) on the entire dataset and do the same, the mutations are there. I've created multiple data frames just trying to get this to work. I thought maybe I had to move the variables again and then the mutations would show up, but that hasn't been the case.
Now I'm also running into the issue of R only applying one mutation. If I try to mutate the variable "qualslp" from numerical to character and then do the same to the variable "sex" r gets rid of the mutation on "qualslp" and it reverts back to numerical, while "sex" becomes a character vector.
Code and screenshots provided below.
screenshot of 55 variables with the one mutation (underlined in yellow)
sleepdata_nvars <-select(sleepdata,
age, sex, alchohol, smokenum, liteslp, wakenite, medhelp, qualslp, depress, ess)
sleepdata_nvars <- mutate(sleepdata,
sex = as.character(sex),
sex = fct_recode(sex,
"female" = "0",
"male" = "1"))
sleepdata_nvars <- mutate(sleepdata,
liteslp = as.character(liteslp),
liteslp = fct_recode(liteslp,
"yes" = "1",
"no" = "2"))
sleepdata_nvars <- mutate(sleepdata,
wakenite = as.character(wakenite),
wakenite = fct_recode(wakenite,
"yes" = "1",
"no" = "2"))
sleepdata_nvars <- mutate(sleepdata,
medhelp = as.character(medhelp),
medhelp = fct_recode(medhelp,
"yes" = "1",
"no" = "2"))
sleepdata_nvars <- mutate(sleepdata,
qualslp = as.character(qualslp),
qualslp = fct_recode(qualslp,
"very poor" = "1",
"poor" = "2",
"fair"= "3",
"good" = "4",
"very good" = "5",
"excellent" = "6"))
Following on from the comment by @MichaelDewar I think the issue is that you should just use one
mutate
statement, and chain it to the originalselect
.