0

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)

screenshot of the data frame (underlined in yellow) containing the 10 variables that I want to use the mutate function on

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"))
  • Welcome to Stack Overflow. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data in a plain text format - for example the output from `dput(yourdata)`. We cannot copy/paste data from images. – neilfws Dec 15 '20 at 23:56
  • 1
    Use `mutate(sleepdata_nvars, ...`. Your current code keeps going back to the original instead of continuing from the modified dataset. – Michael Dewar Dec 15 '20 at 23:59
  • @MichaelDewar This worked, thank you so much! – Vanessa M Dec 16 '20 at 00:10

1 Answers1

1

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 original select.

sleepdata_nvars <- sleepdata %>%
  select(age, sex, alchohol, smokenum, liteslp, 
         wakenite, medhelp, qualslp, depress, ess) %>%
  mutate(sex = as.character(sex),
         sex = fct_recode(sex,
                          "female" = "0",
                          "male" = "1"),
         liteslp = as.character(liteslp),
         liteslp = fct_recode(liteslp,
                              "yes" = "1",
                              "no" = "2"),
         wakenite = as.character(wakenite),
         wakenite = fct_recode(wakenite,
                               "yes" = "1",
                               "no" = "2"),
         medhelp = as.character(medhelp),
         medhelp = fct_recode(medhelp,
                              "yes" = "1",
                              "no" = "2"),
         qualslp = as.character(qualslp),
         qualslp = fct_recode(qualslp,
                              "very poor" = "1",
                              "poor" = "2",
                              "fair"= "3",
                              "good" = "4",
                              "very good" = "5",
                              "excellent" = "6")
)
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Right, yes. My professor and TA both demonstrate that version of doing this as it's more efficient. The reason I didn't is that I'm still new to and very uncomfortable using R, so I like to do things the long way almost as a way of spelling it out for myself. But hopefully, I'll become more confident and efficient in coding as time goes on! Thank you! – Vanessa M Dec 16 '20 at 02:22