简体   繁体   English

如何使用write.table格式化输出

[英]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. 我试图使用R中的MixSim包中的点作为旧的Fortran程序中的采样点,因为我喜欢MixSim更好地创建采样点的方式,但是我正在使用Fortran程序来模拟跨多个级别的植被数据。 Beta多样性,Alpha多样性等。

I generate my data in MixSim by: 我通过以下方式在MixSim中生成数据:

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,这就是我得到的

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. 但是我需要我的输出看起来完全像这样,以便Fortran程序接受它。

***** 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: 我应该指出,我确切地知道如何通过以下方式对r进行舍入:

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

Is my best bet going to be simply using write.table and then formatting "by hand". 我最好的选择是简单地使用write.table然后格式化“手工”。 Most of my models will then be created in a loop that I wrote in Fortran. 然后,将在我用Fortran编写的循环中创建我的大多数模型。 I will only need to generate maybe a few dozen models in MixSim and then format them if that is the case. 我只需要在MixSim中生成几十个模型,然后格式化它们即可。 All models will have considerably more than 10 points. 所有型号的得分将大大超过10分。

(Tried a variety of things with write.table but always got undesired truncation of the decimal values when the trailing figures were nn.00 .) (使用write.table尝试了多种操作,但是当尾随数字为nn.00时,总是会不希望地将十进制值截断。)

Use cat for the file preamble and write.fwf from pkg::gdata: 使用cat作为文件序言, write.fwf从pkg :: gdata中write.fwf

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. 如果需要在LHS上填充,则可以使用width=7或8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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