无法使用R将SPSS数据加载到Mongodb

时间:2015-09-25 12:29:43

标签: r mongodb spss

我正在通过r读取SPSS文件并将数据加载到mongodb中。在将数据导入mongodb时,我遇到了一个问题。我为每个对象获取相同的数据。另外我在R脚本下面提到我正在使用的内容。

library(foreign)
library(surveydata)
library(rmongodb)
path <- "C:/Documents and Settings/sample/Desktop/test.sav"
host <- "xxx.xxx.1.121"
df<-read.spss(path,to.data.frame=TRUE,use.value.labels=FALSE)
sv<-as.surveydata(df,renameVarlabels=TRUE) 
mongo<-mongo.create(host) 
len<-length(df)
buf<-mongo.bson.buffer.create()   
mongo.bson.buffer.append(buf,"survey_ID","test") 
mongo.bson.buffer.append(buf,"survey_type","sample")  
mongo.bson.buffer.append(buf,"time_period","2015")  
for(i in 1:10)
{
  mongo.bson.buffer.start.object(buf,"questions") 
  qcode<-as.character(iconv(names(df[i]),to="UTF8"))   
  qlabel<-as.character(iconv(attr(sv[,i],"variable.labels"),to="UTF8"))
  option_label<-iconv(names(attr(df[,i],"value.label")),to="UTF8")
  option_value<-as.integer(attr(df[,i],"value.label"))
  oplen<-length(option_label)
  if (oplen==0) oplen<-1
  mongo.bson.buffer.append(buf,"qcode",qcode) 
  mongo.bson.buffer.append(buf,"qlabel",qlabel) 
  mongo.bson.buffer.start.array(buf,"option_list") 
  for (j in 1:oplen) 
  {
    loption<-ifelse (is.null(option_label[j]),"",option_label[j]) 
    voption<-ifelse (is.null(option_value[j]),"",option_value[j]) 
    mongo.bson.buffer.start.object(buf,"option")
    mongo.bson.buffer.append(buf,"value",voption) 
    mongo.bson.buffer.append(buf,"label",loption) 
    mongo.bson.buffer.finish.object(buf)
  }
  mongo.bson.buffer.finish.object(buf)
  mongo.bson.buffer.finish.object(buf)
}
b<-mongo.bson.from.buffer(buf)  
mongo.insert(mongo,"test.test_details1",b)

任何人都可以帮助我,我在这里做错了,这样我就可以获得mongodb中不同对象的不同数据

0 个答案:

没有答案