Re: [R] problem of fixed-formated output using sprintf

From: jim holtman <jholtman_at_gmail.com>
Date: Wed 12 Jul 2006 - 03:30:28 EST

Are you sure it is the sprintf vs. write.table? I constructed a dataframe of your size and it took less than 1 second to do the sprintf:

> system.time(df.fmt <- sprintf("%2s%2s%4.2f", df$V1, df$V2, df$V3))
[1] 0.70 0.00 0.73 NA NA
> str(df)

`data.frame': 67944 obs. of 34 variables:

 $ V1 : chr  "0.2655" "0.3721" "0.5729" "0.9082" ...
 $ V2 : chr  "0.2655" "0.3721" "0.5729" "0.9082" ...
 $ V3 : num  0.0642 0.5316 0.5466 0.2798 0.4931 ...
 $ V4 : num  0.8430 0.8279 0.1552 0.5153 0.0725 ...
 $ V5 : num  0.922 0.780 0.811 0.847 0.223 ...


If you want to write this string out to a file, then use 'cat'. If you add '\n' to the sprintf, you will get something like this:

> cat(df.fmt[1:5])

0.26550.26550.06

 0.37210.37210.53
 0.57290.57290.55
 0.90820.90820.28
 0.20170.20170.49

>

On 7/11/06, YIHSU CHEN <yschen@jhu.edu> wrote:
>
> Dear R users:
>
> I'm trying to generate a output file with fixed format using function
> "sprintf" in R. However, the execution time in R is very long even the toy
> data (smaller size df) seems to work fine. The syntax that I used is as
> follows:
>
> df.fmt <- sprintf("%2s%2s%2.4f", df$v1, df$v2, df$v3)
> write.table(df.fmt, output.name,...)
>
> The actual dataset is a df with the dimention of 67944 by 34. I'm
> wondering whether there is an elegant way of doing it. I would like output
> in a txt file.
>
> Many thanks.
>
>
> Yihsu Chen
> The Johns Hopkins University
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Jul 12 03:34:14 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 12 Jul 2006 - 04:18:03 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.