简体   繁体   English

用car重新编码变量-意外变量(.x值)被视为NA

[英]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. 警告消息:被视为NA的未替换值与.x不兼容。 Please specify replacements exhaustively or supply .default 请详尽指定替代品或提供.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. 变量的类是integer ,没有缺失值。

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 . 该警告消息是由于使用了dplyr (可能是OP加载了软件包?)而不是carrecode This happens when dplyr::recode masks the same function in car . dplyr::recode屏蔽car的相同功能时, dplyr::recode发生这种情况。 We can specify the package name before the function as a remedy ie car::recode 我们可以在函数之前指定包名称作为补救措施,例如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复制警告

 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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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