I am working with a data set where I have to recode variables so that Never and Rarely =0, Sometimes and Always as 1, and Not Applicable as NA. For reference, the numbering scheme for the code is that 1=Never, 2=Rarely, 3=Sometimes, 4=Always, and 5= Not Applicable. Should I change the numeric variables before renaming them or change the character variables into numeric ones? I'm at an impasse and could use help on what code to use.
Asked
Active
Viewed 760 times
-2
-
it does not matter- check out case_when() function! – yuliaUU Jul 02 '22 at 17:50
-
Could you please share some reproducible data using `dput`? So we can help you better. – Quinten Jul 02 '22 at 18:04
-
2I'm a little confused: you say that `Never` and `Rarely` should be 0, but then you say `1=Never` and `2=Rarely`. I think small sample data (using `dput(..)` or `data.frame(..)`) is critical here, and please be clear about your expected output given that sample data. – r2evans Jul 02 '22 at 18:15
1 Answers
0
The problem
You have a vector (or a data frame column) x
with values 1 through 5, eg:
x <- c(1,2,3,4,5,4,3,2,1)
You want to recode 1 and 2 to 0, 3 and 4 to 1, and 5 to NA.
Solution in base R
values <- list(`1` = 0, `2` = 0, `3` = 1, `4` = 1, `5` = NA)
x <- unname(unlist(values[x]))
[1] 0 0 1 1 NA 1 1 0 0
Solution with dplyr::recode()
values <- list(`1` = 0, `2` = 0, `3` = 1, `4` = 1, `5` = NA_real_)
x <- dplyr::recode(x, !!!values)
[1] 0 0 1 1 NA 1 1 0 0

Caspar V.
- 1,782
- 1
- 3
- 16
-
How can I apply this to my data set now that has those numbers? Do I replace 'values' with the data table I have? – Sebastian Pintea Jul 03 '22 at 22:43
-
`x` is your data; if you currently have your variable in `df$happiness`, then replace `x` with `df$happiness`. Eg: `df$happiness <- unname(unlist(values[df$happiness]))`. `values` is the translation 'dictionary'; it holds the 'before = after' pairs. – Caspar V. Jul 03 '22 at 22:47
-
Gotcha. So when I do the c(1,2,3,4,5) function, it only looks at the first 5 rows. My data set has 1704 rows of those numbers. How do I manipulate it so the vector has those numbers but for all the rows if that makes sense. – Sebastian Pintea Jul 05 '22 at 15:09
-
When A is your data frame, and B is the column in your data frame that holds the numbers: `A$B <- unname(unlist(values[A$B]))`. If that doesn't help you, share a part of your data set, for instance with `str(dataset)`, `dput(dataset)` or even a screenshot. – Caspar V. Jul 05 '22 at 21:55
-
@SebastianPintea Please take the time to check out the answers to [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Caspar V. Jul 05 '22 at 22:08