3

I'm currently working on a project, and I'm using R to do the data handling and analysis. My data originates from a survey of approximately 3.500 persons with around 200 variables.

At the moment I'm stuck with a problem. I'm looping over several numeric variables to revalue them in the opposite order, such that a higher value is associated with a higher degree of satisfaction.

I'm declaring the variables, which I'm trying to revalue using the "PLYR" package, in varlist.

varlist = c("q16","q17","q18","q19","q20","q21","q22","q23","q24")

Then I'm looping over the variables in varlist.

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")))
}

I get the following error:

The following from values were not present in x: 6, 5, 4, 3, 2, 1  

I've a hard time figuring out, what the error means. And I've found another question, which is similar Updated: Plyr rename() not recognizing identical 'x'; Error: The following `from` values were not present in `x`: I've tried a lot of things, I've tried removing the double quotes, changed the loop to data_rename$i etc.

The a subset of the data is available here: https://www.dropbox.com/s/rsx4coeg4phgcri/data_rename.txt?dl=0

Hope someone is able to help me

Community
  • 1
  • 1

2 Answers2

4

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.

bgoldst
  • 34,190
  • 6
  • 38
  • 64
0

Consider using dplyr instead of plyr:

library(dplyr)
data_rename %>%
    mutate_each(funs(7 - .), q16:q24)

Of course 7-. works in your given example, but not for more complex revalue-operations. But you are free to define your own function and use it within "funs"

MarkusN
  • 3,051
  • 1
  • 18
  • 26