Skip to content
Prev 86172 / 398526 Next

writing a file using both cat() and paste()

Taka Matzmoto wrote:
First, you might want to avoid using "vector", as that is the name of an 
R function. Say you have a 30 element data vector as above. If you 
wanted to write a fairly general function to do this, here is a start:

vector2file<-function(x,file="",values.per.line=5) {
  if(nchar(file)) sink(file)
  cat(deparse(substitute(x)),"<-c(\n")
  xlen<-length(x)
  for(i in 1:xlen) {
   cat(x[i])
   if(i<xlen) cat(",")
   if(i%%values.per.line == 0) cat("\n")
  }
  cat(")")
  if(i%%values.per.line) cat("\n")
  if(nchar(file))sink()
}

Jim