The R Project SVN R-packages

Rev

Rev 1640 | Rev 4680 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1640 Rev 1650
Line 4... Line 4...
4
  do.call(paste("writeForeign",package,sep=""),
4
  do.call(paste("writeForeign",package,sep=""),
5
          c(list(df=df,datafile=datafile,codefile=codefile),...))
5
          c(list(df=df,datafile=datafile,codefile=codefile),...))
6
 
6
 
7
}
7
}
8
 
8
 
-
 
9
## we want ASCII quotes, not UTF-8 quotes here
-
 
10
adQuote <- function(x) paste("\"", x, "\"", sep = "")
-
 
11
 
9
writeForeignSPSS<-function(df,datafile,codefile,varnames=NULL){
12
writeForeignSPSS<-function(df,datafile,codefile,varnames=NULL){
10
 
13
 
11
  dfn<-lapply(df, function(x) if (is.factor(x)) as.numeric(x) else x)
14
  dfn<-lapply(df, function(x) if (is.factor(x)) as.numeric(x) else x)
12
  write.table(dfn, file=datafile,row=FALSE,col=FALSE)
15
  write.table(dfn, file=datafile,row=FALSE,col=FALSE)
13
 
16
 
Line 21... Line 24...
21
  }
24
  }
22
 
25
 
23
  cat("DATA LIST FILE=",dQuote(datafile)," free\n",file=codefile)
26
  cat("DATA LIST FILE=",dQuote(datafile)," free\n",file=codefile)
24
  cat("/", varnames," .\n\n",file=codefile,append=TRUE)
27
  cat("/", varnames," .\n\n",file=codefile,append=TRUE)
25
  cat("VARIABLE LABELS\n",file=codefile,append=TRUE)
28
  cat("VARIABLE LABELS\n",file=codefile,append=TRUE)
26
  cat(paste(varnames, dQuote(varlabels),"\n"),".\n",file=codefile,append=TRUE)
29
  cat(paste(varnames, adQuote(varlabels),"\n"),".\n",file=codefile,append=TRUE)
27
  factors<-sapply(df,is.factor)
30
  factors<-sapply(df,is.factor)
28
  if (any(factors)){
31
  if (any(factors)){
29
    cat("\nVALUE LABELS\n",file=codefile,append=TRUE)
32
    cat("\nVALUE LABELS\n",file=codefile,append=TRUE)
30
    for(v in which(factors)){
33
    for(v in which(factors)){
31
      cat("/\n",file=codefile,append=TRUE)
34
      cat("/\n",file=codefile,append=TRUE)
32
      cat(varnames[v]," \n",file=codefile,append=TRUE)
35
      cat(varnames[v]," \n",file=codefile,append=TRUE)
33
      levs<-levels(df[[v]])
36
      levs<-levels(df[[v]])
34
      cat(paste(1:length(levs),dQuote(levs),"\n",sep=" "),file=codefile,append=TRUE)
37
      cat(paste(1:length(levs),adQuote(levs),"\n",sep=" "),file=codefile,append=TRUE)
35
    }
38
    }
36
    cat(".\n",file=codefile,append=TRUE)
39
    cat(".\n",file=codefile,append=TRUE)
37
  }
40
  }
38
  cat("\nEXECUTE.\n",file=codefile,append=TRUE)
41
  cat("\nEXECUTE.\n",file=codefile,append=TRUE)
39
}
42
}