0

I have several files in Fortran and want to know what procedures are needed to work with them, especially to read the data, set the width of the columns and get a data frame with which to work in R. Later, I want to perform some function to these data, whether the sum of all values ​​in a column, or, get the mean, median, etc.

2 Answers2

0

There are several practices to read Fortran from R.

However, they can be considered as best practices, some of the following:

First Procedure

Download the file Check which you are working in the directory where the file is located

Read a few lines to check the width of each column so you can fix it.

    1. Make the connection to the file

    con <- file("yourfileraw.for")  

2 . Determines some lines you want to read to check the width of each column so you can fix it (the number of lines in this case are ten, as noted in the code).

    readLineFortran  <- readLines(con, 10)

3 Print the lines you want to read to verify its width

    readLineFortran

    close(con)

a. Print the line that shows the data you need to read to determine the total number of characters that must be fixed

    rowStartinData <- readLineFortran[5]

b. Find the characters of the same type that are concatenated are separators (blanks, bars, commas) or strings (numeric, character, logical, etc.). Locate the coordinates that let you know where each chain ends, whether separators or character strings.

In this case applying a regular expression pattern is used, special care must be taken to give the proper pattern, both beginning and ending column or chain as well as the elements to be taken into account, remembering to mention the type of separator used in this case a concatenation of spaces of varying size (m= 3 or x number, n= 2 or y number).

    matchConcat <- unlist(gregexpr(pattern  = "(m|n|-0|[[:space:]]+)", 
    text = rowStartinData, ignore.case = FALSE, 
    perl = TRUE, useBytes = FALSE))

4 . Create an empty vector of the same length which was created to find the limits of each separator and string.

    widths <- vector(mode = "numeric", length = length(matchConcat))

5 . Add a function to determine the size of each string or column without distinction between the spacers and strings, from data provided by the regular expression and the empty vector.

    for(i in 1:length(matchConcat)-1){
    widths[i] <- matchConcat[i] - matchConcat[i + 1]
    }

6 . Define which column type will be selected to perform the functions (taking into account its length(x,y,z)).

    widths[abs(widths) == x] <- x  
    widths[abs(widths) == y] <- y  
    widths[abs(widths) == z] <- z

7 . Read the file and the data and fixed length columns.

    dataFixed <- read.fwf("youfilerow.for", widths = widths, skip = x)

8 . Get results through a function, either by column or root or both.

    xFunction(dataFixed[i,j])
Community
  • 1
  • 1
0

After downloading the file. I think it is necessary to divide the variables differently.

In this example, I write the code to show how you can work the file.

Second procedure for working with fortran files
1. Read and fix fortran file (remember that all spaces have, even if the data begins with one of them)

yourworktempfile <- read.fwf("namefortranfile.for", widths=c(V1,V2,...Vn))

2 . Extracting the column to which want to work

yourworktempdata <- yourworktempfile[,Vn]

3 . Turn extracted data to a matrix

dataextratedmatrix <- matrix(yourworktempdata, 
nrow= Numberows, ncol = Numbercols, byrow =FALSE, dimnames= NULL)

4 . Remove head (only in this case)

datamatrixclean <- dataextratedmatrix[Numberow_Ini:Numberow_End,]

5 . To convert characters as numeric data

numericData <- as.numeric(datamatrixclean)

6 . Execute the 'x' function (mean, sum, etc)

xFunction(numericData)