Try this.  It creates a 'list' that are the parameters for sprintf.  It
writes out the file that I attach as the dump:

### Create a sample data matrix
myMat <- matrix(rnorm(9), 3,3)

# create the format required -- make sure it is wide enough
# for 'fixed' width
xx <- paste(rep("%8.2f", ncol(myMat)), collapse = '')
# create a list for 'do.call(sprintf'
callList <- vector('list', ncol(myMat) + 1)
callList[[1]] <- xx  # store in the format

# add the columns to the list
for (i in seq(ncol(myMat))) callList[[i + 1L]] <- myMat[, i]
callList  # print it out

result <- do.call(sprintf, callList)

# write out the data to a file
writeLines(result, '/temp/file.txt')


Here is what is in the file:

   -0.39    0.45   -0.44
   -0.82   -0.68   -0.43
    2.05   -0.85    0.61



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Fri, Apr 11, 2014 at 2:48 PM, Doran, Harold <[email protected]> wrote:

> I have working code to write a file out as fwf as shown below. I have one
> question to try and automate this I cannot get to work.
>
> I am generating thousands of data files for a simulation to be run outside
> of R and each file varies in its dimensions. So I am trying to write code
> that can write the fwf based on the dimensions of each file generated as
> shown below. I have commented this code with an example to show where I am
> stuck.
>
> ### Create a sample data matrix
> myMat <- matrix(rnorm(9), 3,3)
>
> ### Create the vector of format strings to be equal to the length of the
> columns in myMat
> aa <- rep('%4f', ncol(myMat))
> xx <- paste(aa, sep='', collapse='')
>
> ### Now I could just do this below and it works
> (out <- sprintf(xx, myMat[, 1], myMat[, 2], myMat[, 3]) )
> out <- as.matrix(out) # convert to a character matrix
> dimnames(out) <- list(rep('', nrow(out)), '') # blank row and column names
> noquote(out) ## sink this file to a directory
>
> But, the fact that the dimensions of my matrix vary at each iteration
> means I need to automate this part in the sprint().
>
> myMat[, 1], myMat[, 2], myMat[, 3])
>
> I think that's needed because something like the following does not work
>
> (out <- sprintf(xx, myMat[, 1:3]) )
>
>
> So, I thought about trying smoething like this
> cols <- paste(paste('myMat[, ', 1:ncol(myMat), sep=''), ']', sep='')
> cols <- paste(cols, collapse=', ')
>
> But, this is a string with quotation marks, so I thought using cat() might
> work, but it does not
>
> (out <- sprintf(xx, cat(cols) ) )
>
> Anyone have a suggestion for the right way to do this, this is getting
> messy.
>
> Thank
> Harold
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to