Recode variable with car - unexpectedly variable (.x value) treated as NA

2.3k views Asked by At

My aim is to recode my variable into another variable with an inverted value:

f1_1_recAktuell <- recode(Dat_MonatAktuell$f1_1, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1") 

This code has served me know for more than a year, yet out of a sudden I get this error message:

Warning message: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default

I guess there's something wrong with the data, yet I cannot find out what. The class of the variable is integer, it has no missing values.

Does anyone know what I can do?

Thanks in advance!

1

There are 1 answers

1
akrun On

The warning message is due to use of recode from dplyr (possibly the OP loaded the package?) instead of from car. This happens when dplyr::recode masks the same function in car. We can specify the package name before the function as a remedy i.e. car::recode

v1 <- 1:10
car::recode(v1, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1") 
#[1] 10  9  8  7  6  5  4  3  2  1

Also, we can use

11-v1 
#[1] 10  9  8  7  6  5  4  3  2  1

The warning can be reproduced with dplyr::recode

 dplyr::recode(v1, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1") 
 #[1] "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1" NA                                                  
 #[3] NA                                                   NA                                                  
 #[5] NA                                                   NA                                                  
 #[7] NA                                                   NA                                                  
 #[9] NA                                                   NA                                                  
Warning message:
 Unreplaced values treated as NA as .x is not compatible.
  Please specify replacements exhaustively or supply .default

To get the expected output, we can also use dplyr::recode in this way

dplyr::recode(v1, `1` = 10, `2` = 9, `3` = 8, `4` = 7, 
                 `5` = 6, `6` = 5, `7`=4, `8` = 3, `9` = 2, `10` = 1)
#[1] 10  9  8  7  6  5  4  3  2  1