It's not an error. It's a warning, and it can be suppressed with the 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
I ran the for-loop on your dropbox data and only got one warning message, which referred to values 6, 5, 4, and 3. But the loop worked despite the warning:
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
The culprit is column q24
, which only has values 1 and 2, IOW is missing 3, 4, 5, and 6 (note: I ran the following on the raw file data, i.e. before running the for-loop which revalues the values):
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
So this doesn't look like a problem to me.