[R] read.fortran format

William Dunlap wdunlap at tibco.com
Sat May 28 19:02:44 CEST 2016


>The bit about the decimal leading to a shift in the decimal place
>pointed out by Bill is a bit obscure, though it to is mentioned in the
help file.

I don't think that is how real Fortran formats work.  My memory is that
you only put a dot in the format if there were no dots in your data file
(so you could avoid wasting one of the 80 columns on the punched card
on a dot).

With current gfortran, putting a dot in the data overrides the decimal
place specification in the format.

% cat format.f
      double precision d1, d2
      integer*4 i1
      integer*4 i

 10   format(I2F7.1F7.6)
      do 20 i=1,100
        read(*,10) i1, d1, d2
        write(*,*) i1, d1, d2
 20   continue
      stop
      end

% gfortran format.f -o format.exe
% ./format.exe
1234012340987654
          12   340123.40000000002       0.98765400000000003
123.0123409.7e20
          12   3.0123400000000000        9.7000000000000000E+020
123.012340976e20
          12   3.0123400000000000        97600000000000000.






Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, May 27, 2016 at 10:14 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

> Your rather sarcastic comment about knowledge given by John's mother seems
> inappropriate, given that he told you where his information came from and
> it is the first place you should have looked.
>
> The bit about the decimal leading to a shift in the decimal place pointed
> out by Bill is a bit obscure, though it to is mentioned in the help file.
>
> The "D" format is broken though... the regex template in the processFormat
> embedded function is missing that option. Bill's use of 'F' instead with no
> decimal is the easy workaround, but that is a bug.
> --
> Sent from my phone. Please excuse my brevity.
>
> On May 27, 2016 8:30:49 PM PDT, Steven Yen <syen04 at gmail.com> wrote:
> >That's great, John. Your mother told you when you were born? How am I
> >supposed to know? Thank you both.
> >The following format statement did it!! I just change F5.3 to F5, 5F8.4
> >
> >to 5F8. I also change 2E15.9 to 2A9, and then use the following
> >as.numeric to convert the alphanumerical to numerical. Thank you!!!
> >
> >mydata<-read.fortran("GROUPC.DAT",
> >         list(c("1X","F6","5F8"),
> >              c("1X","5F8","F10"),
> >              c("1X","2F6","3A15","F8","F5","F5"),
> >              c("1X","F7","2A15","F9","F5")),
> >col.names=c("year","w1","w2","w3","w4","w5","v1","v2","v3",
> >"v4","v5","m","chyes","chno","ec","vc","cvc",
> >"pop","ahs","fah","tnh","eq","vq","ups","zm1"))
> >mydata$ec <-as.numeric(mydata$ec)
> >
> >On 5/27/2016 6:33 PM, William Dunlap wrote:
> >> It has been a while since I used Fortran formatted input, but the
> >> following,
> >> without dots in the format, works:
> >>
> >>     > txt <- "1950. .614350 .026834 .087227 .006821 .180001 4.56E-2"
> >>     > print(read.fortran(textConnection(txt), c("f5", "6f8")),
> >digits=10)
> >>         V1      V2       V3       V4       V5       V6 V7
> >>     1 1950 0.61435 0.026834 0.087227 0.006821 0.180001 0.0456
> >>
> >>
> >> If I recall correctly, a dot in the format pushes the decimal point:
> >>
> >>     > print(read.fortran(textConnection(txt), c("f5", "6f8.3")),
> >>     digits=10)
> >>         V1         V2         V3         V4        V5          V6
> >  V7
> >>     1 1950 0.00061435 2.6834e-05 8.7227e-05 6.821e-06 0.000180001
> >4.56e-05
> >>
> >>
> >>
> >> Bill Dunlap
> >> TIBCO Software
> >> wdunlap tibco.com <http://tibco.com>
> >>
> >> On Fri, May 27, 2016 at 3:15 PM, Steven Yen <syen04 at gmail.com
> >> <mailto:syen04 at gmail.com>> wrote:
> >>
> >>     Thanks John. That helped, but I got a mixed of good thing and bad
> >>     thing.
> >>     Good is R does not like the scientific number format "3E15.9" but
> >>     I was
> >>     able to read with alphanumerical format "3A15" (and convert to
> >>     numerical). Bad is R does not like the numbers .1234, .2345
> >>     without the
> >>     zeros before the decimal points. My data look like:
> >>
> >>        1950. .614350 .026834 .087227 .006821 .180001 .084766
> >>
> >>     The first variable was read correctly, followed by six 0's.
> >>
> >>     As the instructions say, this fortran format is approximation at
> >best
> >>     and in this case, a poort approximation.
> >>
> >>     On 5/27/2016 2:21 PM, John McKown wrote:
> >>     > On Fri, May 27, 2016 at 12:56 PM, Steven Yen <syen04 at gmail.com
> >>     <mailto:syen04 at gmail.com>
> >>     > <mailto:syen04 at gmail.com <mailto:syen04 at gmail.com>>>wrote:
> >>     >
> >>     >     Dear fellow R users:
> >>     >     I am reading a data (ascii) file with fortran fixed format,
> >>     containing
> >>     >     multiple records. R does not recognize fortran's record
> >break (a
> >>     >     slash).
> >>     >     I tried to do the following but it does not work. Help
> >>     appreciated.
> >>     >
> >>     >       60
> >>     >
> >FORMAT(1X,F6.0,5F8.6/1X,5F8.4,F10.6/1X,2F6.0,3E15.9,F8.0,F5.2,F5.3
> >>     >           *      /1X,F7.0,2E15.9,F9.4,F5.3)
> >>     >
> >>     >
> >>
> >mydata<-read.fortran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT",
> >>     >     ​​
> >>     >              c("1X","F6.0","5F8.6"/"1X","5F8.4","F10.6"
> >>     >  /"1X","2F6.0","3E15.9","F8.0","F5.2","F5.3"
> >>     >               /"1X","F7.0","2E15.9","F9.4","F5.3"),
> >>     >     ​​
> >>     >
> >col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3",
> >>     >     "v4","v5","v6","z","chyes","chno","ec","vc","cvc",
> >>     >     "pop","ahs","fah","tnh","eq","vq","ups","zm1 "))
> >>     >
> >>     >
> >>     > ​Did you see this from ?read.fortran
> >>     >
> >>     > <quote>
> >>     >
> >>     >  For a single-line record, ‘format’ should be a character
> >vector.
> >>     >    For a multiline record it should be a list with a character
> >>     vector
> >>     >    for each line.
> >>     >
> >>     > </quote>​
> >>     >
> >>     > ​I think (not sure) you need:
> >>     >
> >>     >
> >>
> >mydata<-read.frotran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT",
> >>     >
> >>
>
> >list(c("1X","F6.0","5F8.6"),c("1X","5F8.4","F10.6"),c("1X","2F6.0","3E15.9","F8.0","F5.2","F5.3"),c("1X","F7.0","2E15.9","F9.4","F5.3")).
> >>     > ​
> >>     >
> >col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3",
> >>     > "v4","v5","v6","z","chyes","chno","ec","vc","cvc",
> >>     > "pop","ahs","fah","tnh","eq","vq","ups","zm1 "))
> >>     >
> >>     >
> >>     >
> >>     >
> >>     >
> >>     > --
> >>     > The unfacts, did we have them, are too imprecisely few to
> >>     warrant our
> >>     > certitude.
> >>     >
> >>     > Maranatha! <><
> >>     > John McKown
> >>
> >>
> >>             [[alternative HTML version deleted]]
> >>
> >>     ______________________________________________
> >>     R-help at r-project.org <mailto:R-help at 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 at 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 at 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