[R] Save file as Fixed Width using sprintf()

Frede Aakmann Tøgersen frtog at vestas.com
Sat Apr 12 10:32:26 CEST 2014


Hi

Have a look at the write.fwf ( fixed width format) in gdata package.


> ### load package
> library(gdata)
> 
> ### Create a sample data matrix
> set.seed(4324)
> myMat <- matrix(rnorm(9), 3,3)
> 
> ### cannot get it tp work with matrix so cast to dataframe
> write.fwf(as.data.frame(myMat), file = "", sep = "", colnames = FALSE)
-0.6464857-0.6289709-0.3129283
 1.4342941-0.6378252-1.6057577
 0.6456513 0.2481384-1.1617556
> 
> ### same as before
> write.fwf(as.data.frame(myMat), file = "", width = c(10,10,10), sep = "", colnames = FALSE)
-0.6464857-0.6289709-0.3129283
 1.4342941-0.6378252-1.6057577
 0.6456513 0.2481384-1.1617556
> 
> ### rounding
> write.fwf(as.data.frame(round(myMat, 6)), file = "", width = rep(9, ncol(myMat)), sep = "", colnames = FALSE)
-0.646486-0.628971-0.312928
 1.434294-0.637825-1.605758
 0.645651 0.248138-1.161756
>

With this approach I don't think you can get rid of the space between positive numbers because there should be place for a minus sign for negative values. If that's what you want????


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of jim holtman
> Sent: 11. april 2014 22:39
> To: Doran, Harold
> Cc: r-help at r-project.org
> Subject: Re: [R] Save file as Fixed Width using sprintf()
> 
> 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 <HDoran at air.org> 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]]
> >
> > ______________________________________________
> > R-help at r-project.org 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]]
> 
> ______________________________________________
> R-help at r-project.org 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.




More information about the R-help mailing list