[R] saving output

Jim Lemon jim at bitwrit.com.au
Wed Aug 8 14:00:13 CEST 2007


Lynn Disney wrote:
> I have a question about how to save the output of a logistic regression
> model in some format (delimited, preferably) so that I can manipulate it
> to make nice tables in excel. I have tried print, save, write, none seem
> to do what I want them to. Does anyone have any ideas?
> 
Hi Lynn,
This is an interesting idea that might be useful in the prettyR package. 
You have just volunteered to be a test pilot.

delim.table<-function(x,con="",delim="\t") {
  if(nchar(con)) {
   con<-file(con,"w")
   open.con<-TRUE
  }
  else open.con<-FALSE
  column.names<-names(x)
  if(is.null(column.names)) column.names<-colnames(x)
  have.col.names<-!is.null(column.names)
  row.names<-rownames(x)
  have.row.names<-!is.null(row.names)
  xdim<-dim(x)
  if(have.col.names) {
   cat(delim,file=con)
   for(col in 1:xdim[2])
    cat(column.names[col],delim,sep="",file=con)
   cat("\n",file=con)
  }
  for(row in 1:xdim[1]) {
   if(have.row.names) cat(row.names[row],file=con)
   cat(delim,file=con)
   for(col in 1:xdim[2]) cat(x[row,col],delim,sep="",file=con)
   cat("\n",file=con)
  }
  if(open.con) close(con)
}

test.df<-data.frame(a=sample(0:1,100,TRUE),b=rnorm(100),c=rnorm(100))
delim.table(
  summary(glm(a~b*c,test.df,family="binomial"))$coefficients,
  con="test.csv")

This output should import into Excel with no trouble (it does in 
OpenOffice Calc). Either use sink() or specify a filename as in the example.

Jim



More information about the R-help mailing list