这是不是一个错误。这是一个警告,它可以与warn_missing
argument of revalue()
抑制:
library(plyr);
varlist = c('q16','q17','q18','q19','q20','q21','q22','q23','q24');
data_rename <- do.call(data.frame,setNames(nm=varlist,rep(list(as.character(1:5)),9L)));
data_rename;
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 1 1 1 1 1 1 1 1 1
## 2 2 2 2 2 2 2 2 2 2
## 3 3 3 3 3 3 3 3 3 3
## 4 4 4 4 4 4 4 4 4 4
## 5 5 5 5 5 5 5 5 5 5
## run with warnings
for (i in varlist) data_rename[,i] <- as.numeric(revalue(as.character(data_rename[,i]),c('6'='1','5'='2','4'='3','3'='4','2'='5','1'='6')));
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
## The following `from` values were not present in `x`: 6
data_rename;
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 6 6 6 6 6 6 6 6 6
## 2 5 5 5 5 5 5 5 5 5
## 3 4 4 4 4 4 4 4 4 4
## 4 3 3 3 3 3 3 3 3 3
## 5 2 2 2 2 2 2 2 2 2
## run without warnings
data_rename <- do.call(data.frame,setNames(nm=varlist,rep(list(as.character(1:5)),9L)));
for (i in varlist) data_rename[,i] <- as.numeric(revalue(warn_missing=F,as.character(data_rename[,i]),c('6'='1','5'='2','4'='3','3'='4','2'='5','1'='6')));
data_rename;
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 6 6 6 6 6 6 6 6 6
## 2 5 5 5 5 5 5 5 5 5
## 3 4 4 4 4 4 4 4 4 4
## 4 3 3 3 3 3 3 3 3 3
## 5 2 2 2 2 2 2 2 2 2
我跑在你的Dropbox数据为环,只获得一个警告消息,其中提到值6,5,4和3,但循环工作,尽管警告:
data_rename <- read.table('data_rename.txt',sep=';');
head(data_rename);
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 2 2 3 1 2 2 3 3 2
## 2 1 1 1 1 1 2 1 1 2
## 3 1 2 2 2 2 1 1 1 1
## 4 2 4 2 3 3 3 2 3 1
## 5 2 4 4 3 3 3 3 3 1
## 6 3 3 3 2 2 2 2 2 1
for (i in varlist) data_rename[,i] <- as.numeric(revalue(as.character(data_rename[,i]),c('6'='1','5'='2','4'='3','3'='4','2'='5','1'='6')));
## The following `from` values were not present in `x`: 6, 5, 4, 3
head(data_rename);
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 5 5 4 6 5 5 4 4 5
## 2 6 6 6 6 6 5 6 6 5
## 3 6 5 5 5 5 6 6 6 6
## 4 5 3 5 4 4 4 5 4 6
## 5 5 3 3 4 4 4 4 4 6
## 6 4 4 4 5 5 5 5 5 6
罪魁祸首是列q24
,whic (注意:我在原始文件数据上运行以下内容,即在运行重新计算值的for-loop之前):
sapply(data_rename,function(col) table(col)[as.character(1:6)]);
## q16 q17 q18 q19 q20 q21 q22 q23 q24
## 1 1458 731 922 1591 1327 825 1117 1128 1941
## 2 1582 1664 1694 1616 1875 1814 1679 1707 1879
## 3 390 761 586 418 316 510 570 597 NA
## 4 283 500 455 137 246 477 351 278 NA
## 5 103 152 149 41 53 190 92 77 NA
## 6 4 11 14 17 3 4 11 33 NA
所以这对我来说不是一个问题。
将dput(data_rename [1:20,])的输出添加到你的问题中。 – Roland
另外,你为什么要通过字符表示?只需使用'7 - x'。 – Roland