2016-05-13 96 views
0

我目前正在进行一个项目,并且正在使用R进行数据处理和分析。我的数据来源于约有3.500人的调查,大约有200个变量。以下`from`值不存在于`x`中

目前我遇到了一个问题。我循环了几个数值变量,以相反的顺序重新评估它们,使得更高的值与更高的满意度相关联。

我正在声明变量,我试图使用varlist中的“PLYR”包进行重估。

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

然后我循环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"))) 
} 

我得到以下错误:

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

我有一个很难搞清楚,什么错误意味着。我发现一个问题,这是类似 Updated: Plyr rename() not recognizing identical 'x'; Error: The following `from` values were not present in `x`: 我已经尝试了很多东西,我试图消除双引号,改变了环路data_rename$i

的数据的一个子集可在这里: https://www.dropbox.com/s/rsx4coeg4phgcri/data_rename.txt?dl=0

希望有人能够帮助我

+0

将dput(data_rename [1:20,])的输出添加到你的问题中。 – Roland

+0

另外,你为什么要通过字符表示?只需使用'7 - x'。 – Roland

回答

0

这是不是一个错误。这是一个警告,它可以与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 

所以这对我来说不是一个问题。

+0

嘿Bgoldst! 非常感谢。对于我迟到的回答,我很抱歉。我工作了! –

0

考虑使用plyr的dplyr代替:

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

当然7-。在你给出的例子中工作,但不适用于更复杂的重估操作。但是你可以自由定义你自己的功能,并在“乐趣”中使用它

+0

Hey MarkusN。这看起来像一个伟大而有效的方式去实现它! –

相关问题