简体   繁体   中英

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

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!

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 ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM