简体   繁体   中英

How to format output with write.table

I apologize if this is an easy question. I am trying to use the points from the MixSim package in R to act as the sampling points in an old Fortran program because I like the way that MixSim creates sampling points better, but am using the Fortran program to simulate vegetation data across many levels of beta diversity, alpha diversity, etc.

I generate my data in MixSim by:

d=MixSim(BarOmega=0.000,MaxOmega=0.000,K=4,p=3,ecc=0.99,int=c(10,90),PiLow=0.1)
m=simdataset(n=10,Pi=d$Pi,Mu=d$Mu,S=d$S)

And if I do use write.table, this is what I get

write.table(m$X,file="example.txt",quote=F,row.names=F)
V1 V2 V3
87.540626647788 62.8444539443256 17.0026406651813
83.9939847940881 65.0069747775257 18.8676229149976
84.4477456535804 63.6892673685408 18.6384437248469
84.7684968694547 65.4610993744652 17.6252989584773
13.4600970937604 16.9988156469822 49.6810813619893
23.9952555783055 18.6598302958281 48.4204641715953
17.0523647853253 11.518037157788 43.0417655739052
57.5107395863171 40.4872578216636 24.938188234695
11.8320140526743 52.9077915021041 34.5723480775864
12.8754032313702 53.1795899126135 34.1309377040482

But I need my output to look exactly like this for the Fortran program to accept it.

***** SAMPLING PATTERN FILE

 50   3   1          0.0000
50
   87.54    62.84    17.00
   83.99    65.00    18.86
   84.44    63.68    18.63
   84.76    65.46    17.62
   13.46    16.99    49.68
   23.99    18.65    48.42
   17.05    11.51    43.04
   57.51    40.48    24.93
   11.83    52.90    34.57
   12.87    53.17    34.13

I should note, that I know exactly how to do the rounding in r by:

m=round(m$X,digits=2)

Is my best bet going to be simply using write.table and then formatting "by hand". Most of my models will then be created in a loop that I wrote in Fortran. I will only need to generate maybe a few dozen models in MixSim and then format them if that is the case. All models will have considerably more than 10 points.

(Tried a variety of things with write.table but always got undesired truncation of the decimal values when the trailing figures were nn.00 .)

Use cat for the file preamble and write.fwf from pkg::gdata:

cat(top, file='out.txt')
install.packages('gdata')
gdata::write.fwf(signif(dat,4), file = "out.txt", append = TRUE, quote = FALSE, sep = "\t", 
                   colnames = FALSE)

-------result----------
***** SAMPLING PATTERN FILE

 50   3   1          0.0000
50
87.54   62.84   17.00
83.99   65.01   18.87
84.45   63.69   18.64
84.77   65.46   17.63
13.46   17.00   49.68
24.00   18.66   48.42
17.05   11.52   43.04
57.51   40.49   24.94
11.83   52.91   34.57
12.88   53.18   34.13

If you need padding on the LHS you can use width=7 or 8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM