Turning a String Variable into Numeric and removing some values

Hi all. I am struggling to figure how to action what I believe should be a pretty simple task.

I have a variable that is supposed to be a scale (numeric) measure. Members of the public have been asked to provide a rating between 0 and 10. The higher the score, the more satisfied they believe to be. However, there is additionally responses that are "don't know" and "refusal". I would like to simply change this variable so that it can be numeric between 0 and 10, which means removing the two character groups.

Below is a table of the variable in it's current form. Any helps would be appreciated as to how it can simply be turned into a numeric variable between 0 and 10.

Thanks in advance

Hi, as a beginner in R, I recommend studying about R's factor datatype, they will come up often, as in your case.

the tidyverse includes a great package called forcats for manipulating factors.
After you learned the basics from the above tutorial, it can be handy to refer back to this cheat sheet:

alternatively, a straight solution would be of this type:

example_str <- c("not a value",rep("0 - exdis",3),rep("10 - ex sat",4),rep("1",5),rep("6",2))


my_str_recoded <- gsub(x =example_str ,
                      pattern = "0 - exdis",
                      replacement = "0")
my_str_recoded <- gsub(x =my_str_recoded ,
                       pattern = "10 - ex sat",
                       replacement = "10")


table(my_vals,useNA ="ifany")

my_vals_no_na <- my_vals[which(!is.na(my_vals))]

table(my_vals_no_na,useNA ="ifany")

Thanks for recommending the books and will give the code a try. Ironically, I am also having a problem installing this tidyverse I have heard so much of! Every time I try and install it R Studio just crashes and I have to reboot. The joy!

Thanks again for the recommendations and the code.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.