[R] Write text file in Fortran format

Avi Gross @v|@e@gro@@ @end|ng |rom gm@||@com
Wed Sep 21 19:22:51 CEST 2022


Javad,

It would help if you stopped calling it FORTRAN format. I doubt anyone
cares about names as compared to what kind of structure it holds.

It likely is some variant on a file commonly used in R such as one that
uses tabs or whitespace. It may have lines above with headers or comments
and you likely can read it into an R data.frame with some twiddling.

Once there, you can make your various adjustments assuming it got read in
correctly. Look at it before randomly chopping out parts you think are
there.

Then you can save the file in various formats. But note, if you want weird
things added such as a line of underscores below a header line, that may
not be standard. You can do such things, perhaps, in stages you combine
into a final output file.

Note if your files are in some well-known format, there likely may be a
package that handles that for you. If your file has a name with a suffix
like .TSV or .DAT that may provide a hint.

FORTRAN is an evolving language but I doubt it reads or writes one unique
format. You want a compatible output file whatever it is.

Avi

On Wed, Sep 21, 2022, 12:16 PM javad bayat <j.bayat194 using gmail.com> wrote:

> Dear Rasmus;
> I have no knowledge of the FORTRAN language. The text file that has been
> attached is a model's output file and I know that the format is in FORTRAN.
> I want to write a text file exactly similar to the attached text file using
> R programming.
> The steps below explain my goal:
> 1- Read the text file without the first 8 and last 2 rows as a dataframe.
> 2- Double the 3rd column values (or multiply by specific number).
> 3- Insert the removed first 8 rows of the original text file as header in
> the dataframe.
> 4- Insert the removed last 2 rows of the original text file at the end of
> dataframe.
> 5- Write the dataframe as a new text file exactly similar to the original
> text file.
>
> I have used excel but when I save it as text file, the format changes and
> is not similar to the attached text file.
> Sincerely
>
> On Wed, 21 Sep 2022, 18:17 Rasmus Liland, <jral using posteo.no> wrote:
>
> > Dear Javad,
> >
> > Perhaps you were looking to read the
> > table in Air.txt (is this Fortran
> > format?) into R?
> >
> >         b <- readLines("Air.txt")  # The text MIME attachment w/Mailman
> > footer ...
> >         b <- b[1:(which(b=="")-1)]  # Remove the odd Mailman footer (at
> > end of df)
> >         idx <- max(grep("^\\*", b))+1  # Start of df after header uline
> >         header <- tolower(strsplit(gsub("  +", "_", gsub("\\*", "",
> > b[idx-2])), "_")[[1]])
> >         b <- read.table(text=b[-(1:idx)], header=F)
> >         colnames(b) <- header[header!=""]
> >         b <- b[,sapply(b, \(i) length(unique(i)))>1]  # Remove constant
> > cols
> >
> > str(b)
> >
> >         'data.frame':   31324 obs. of  6 variables:
> >          $ x           : num  583500 584000 584500 585000 585500 ...
> >          $ y           : num  3018700 3018700 3018700 3018700 3018700 ...
> >          $ average conc: num  32.8 33.1 33.4 33.5 33.6 ...
> >          $ zelev       : num  0 0 0 0 0 0 0 0 0 0 ...
> >          $ zhill       : num  0 0 0 0 0 0 0 0 0 0 ...
> >          $ date(conc)  : int  16101706 16101706 16101706 16101706
> 16101706
> > ...
> >
> > Best,
> > Rasmus
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list