1

I have a variable from a data frame, which contains the following statement: "Immigrants should be required to adjust to the customs of Germany". The categories of the variable are strongly agree, agree, neither, disagree, strongly disagree. Here is a frequency table of the variable:

> count(dat, 'c2a')
                c2a freq
1             agree  201
2          disagree  214
3           neither  156
4         no answer   15
5    strongly agree   65
6 strongly disagree  138

I would like to recode the variable from text to numbers. I do it the following way:

library(dplyr)
dat$c2a <- recode(dat$c2a, 'strongly disagree' = 1, 'disagree' = 2, 
                  'neither' = 3, 'agree' = 4, 'strongly agree' = 5)

But then I get the following warning message from R: Warning message: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default

How can I specify that all values of c2a which are different from 1 or 2 or 3 or 4 or 5 are NAs?

1 Answers1

3

The values are already NA to make it explicit and make warning go away add .default argument in recode.

dplyr::recode(dat$c2a,'strongly disagree' = 1, 'disagree' = 2, 'neither' = 3, 
              'agree' = 4, 'strongly agree' = 5, .default = NA_real_)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213