Re: [R] Write text file in Fortran format
Javad, After reading the exchanges, I conclude you are asking a somewhat different question than some of us expected and I see some have zoomed in on what you seem to want. You seem to want to make a very focused change and save the results to be as identical as what you started with. You also note you really have no idea about the process that created the file or uses it so it is hard to know if just changing parts results in a valid file. So maybe this looks like not one file but three files. You seem to have a header region of N lines you want kept intact but also placed in the output untouched. You seem to have a few lines at the end you also seem to want to leave untouched. You have lots of lines in the middle you want treated as if it contains multiple columns of data in some format that looks like it is whitespace-separated, or maybe tabs. And I think you want to do this transformation perhaps only once so a general purpose solution is not required. So how about you get your favorite text editor or tool and extract the first N lines from the file and place it in a file called HEADER. You may also then delete it from a copy of the original file if you wish. Similarly, extract the last lines you want to keep from the file and place it in a file called FOOTER. What is left behind in a copy of the file should then be something people here might easily work with. You can use one of many methods as you wish to read the data into some kind of data.frame and supply names for the columns and whatever. You can make your changes to what seems like one column. You can save the output conceptually to use the same format as the input and place it in a file called NEW_DATA. Note no actual files are necessarily needed but conceptually you now have some text in places called HEADER, NEW_DATA and FOOTER and you can combine them carefully (as in no blank lines or two lines concatenated without a newline) and get a new output file that should look like the input. If your data format in the middle section uses something like a tab, this should work. If it uses whitespace and perhaps a fixed-width per line, you have an issue to deal with if your changes need more room than is available or need adjustment to calculate how many spaces to remove or add to center or line up the columns visible. I will end by saying some others have offered variations that are probably just as reasonable. The problem is we may suggest you skip the first lines and it turns out you want to preserve them. Most R functions and packages I know about have no interest in preserving what effectively are often just comments but it seems easy enough to do. Your lines at the end are a bigger problem if you use standard commands to read in the data BECAUSE many such programs do a look-ahead at perhaps the first thousand lines to try to determine the number of columns and the type of each. Your extra lines at the end may perturb that and there may be weird results in your data at the end or even things like an integer column being considered to be character. -Original Message- From: R-help On Behalf Of javad bayat Sent: Thursday, September 22, 2022 6:35 AM To: Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Write text file in Fortran format These 2 lines were at the end of the text file, which I have attached but I had removed them to read the text file in R. Just like the first 8 line that start with asterisk (*). On Thu, 22 Sep 2022, 12:21 Rui Barradas, wrote: > Hello, > > Are those lines at the begining of the file? > > Rui Barradas > > Às 06:44 de 22/09/2022, javad bayat escreveu: > > Dear all; Many thanks for your useful comments and codes. > > I tried both Rui's and Jim's codes. > > Jim's codes gave an error as below: > > "Error in substr(inputline, 1, begincol3 - 1) : > >object 'inputline' not found" > > I don't know what's wrong. > > The Rui's codes worked correctly for the attached file. But I have > > edited that file and removed 2 last lines from the file because I > > could not read it into R. > > These 2 lines were: > > " > > ** CONCUNIT ug/m^3 > > ** DEPUNIT g/m^2 > > " > > When I tried to run the code for my file that has these 2 lines at > > the > end, > > it gave me this error: > > " > >> df1 <- read.table(text = txt_table) > > Error in read.table(text = txt_table) : no lines available in input > > " > > The codes before the "df1 <- read.table(text = txt_table)" were run > > correctly. > > Sincerely > > > > > > > > On Thu, Sep 22, 2022 at 6:58 AM javad bayat > wrote: > > > >> Dear all; > >> I apologise, I didn't know that I have to cc the list. > >> Thank you Mr Rui for reminding m
Re: [R] Write text file in Fortran format
Hello, Maybe the following solves the problem. I hope it does if the mailman footer is no longer there. # this path depends on the OP's system path <- "~/Temp" fl <- list.files(path, pattern = "Air.txt", full.names = TRUE) # read the file as text txt <- readLines(fl) # find lines starting with an asterisk i <- grep("^\\*", txt) # keep the other lines txt_table <- txt[-i] txt_table <- trimws(txt_table) df1 <- read.table(text = txt_table) # multiply 3rd column by 2 df1[[3]] <- 2*df1[[3]] # format the table's lines to write them back to file fmt <- "%14.5f %13.5f %13.5f %8.2f %8.2f %8.2f %7s %8s %10s %9s %9d" new_data <- sprintf(fmt, df1[[1]], df1[[2]], df1[[3]], df1[[4]], df1[[5]], df1[[6]], df1[[7]], df1[[8]], df1[[9]], df1[[10]], df1[[11]]) # assemble the header and the table txt_new <- c(txt[i], new_data) # write to text file fl2 <- file.path(dirname(fl), "Air2.txt") writeLines(txt_new, fl2) Hope this helps, Rui Barradas Às 11:35 de 22/09/2022, javad bayat escreveu: These 2 lines were at the end of the text file, which I have attached but I had removed them to read the text file in R. Just like the first 8 line that start with asterisk (*). On Thu, 22 Sep 2022, 12:21 Rui Barradas, wrote: Hello, Are those lines at the begining of the file? Rui Barradas Às 06:44 de 22/09/2022, javad bayat escreveu: Dear all; Many thanks for your useful comments and codes. I tried both Rui's and Jim's codes. Jim's codes gave an error as below: "Error in substr(inputline, 1, begincol3 - 1) : object 'inputline' not found" I don't know what's wrong. The Rui's codes worked correctly for the attached file. But I have edited that file and removed 2 last lines from the file because I could not read it into R. These 2 lines were: " ** CONCUNIT ug/m^3 ** DEPUNIT g/m^2 " When I tried to run the code for my file that has these 2 lines at the end, it gave me this error: " df1 <- read.table(text = txt_table) Error in read.table(text = txt_table) : no lines available in input " The codes before the "df1 <- read.table(text = txt_table)" were run correctly. Sincerely On Thu, Sep 22, 2022 at 6:58 AM javad bayat wrote: Dear all; I apologise, I didn't know that I have to cc the list. Thank you Mr Rui for reminding me. Let me clarify more. 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. Maybe I have removed the last 2 lines, but at the end it had 2 lines similar the first 8 lines which starts with asterisk. 2- Double the 3rd column values (or multiply by specific number). 3- Insert the removed first 8 lines of the original text file as header in the dataframe. 4- Insert the removed last 2 lines 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. I will try all your codes in a few hours, cause I am out of office. Sincerely yours. On Thu, 22 Sep 2022, 04:20 Richard O'Keefe, wrote: Oh, so you want to WRITE a file *like* that. Use ?sprintf. (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8) Fortran sprintf 1X a space 3X 3 spaces F13.5 %13.5f F8.2%8.2f A5 %5s A8 %8s I8 %8d 3(...) the translation of ... written 3 times We can simplify 1X,F13.5 to F14.5 Here's the sprintf() equivalent of the Fortran format, except you'll need to change the dots to spaces. "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n On Thu, 22 Sept 2022 at 04:17, javad bayat 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, 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
Re: [R] Write text file in Fortran format
These 2 lines were at the end of the text file, which I have attached but I had removed them to read the text file in R. Just like the first 8 line that start with asterisk (*). On Thu, 22 Sep 2022, 12:21 Rui Barradas, wrote: > Hello, > > Are those lines at the begining of the file? > > Rui Barradas > > Às 06:44 de 22/09/2022, javad bayat escreveu: > > Dear all; Many thanks for your useful comments and codes. > > I tried both Rui's and Jim's codes. > > Jim's codes gave an error as below: > > "Error in substr(inputline, 1, begincol3 - 1) : > >object 'inputline' not found" > > I don't know what's wrong. > > The Rui's codes worked correctly for the attached file. But I have edited > > that file and removed 2 last lines from the file because I could not read > > it into R. > > These 2 lines were: > > " > > ** CONCUNIT ug/m^3 > > ** DEPUNIT g/m^2 > > " > > When I tried to run the code for my file that has these 2 lines at the > end, > > it gave me this error: > > " > >> df1 <- read.table(text = txt_table) > > Error in read.table(text = txt_table) : no lines available in input > > " > > The codes before the "df1 <- read.table(text = txt_table)" were run > > correctly. > > Sincerely > > > > > > > > On Thu, Sep 22, 2022 at 6:58 AM javad bayat > wrote: > > > >> Dear all; > >> I apologise, I didn't know that I have to cc the list. > >> Thank you Mr Rui for reminding me. > >> Let me clarify more. > >> 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. > >> Maybe I have removed the last 2 lines, but at the end it had 2 lines > >> similar the first 8 lines which starts with asterisk. > >> 2- Double the 3rd column values (or multiply by specific number). > >> 3- Insert the removed first 8 lines of the original text file as header > in > >> the dataframe. > >> 4- Insert the removed last 2 lines 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. > >> I will try all your codes in a few hours, cause I am out of office. > >> Sincerely yours. > >> > >> > >> > >> On Thu, 22 Sep 2022, 04:20 Richard O'Keefe, wrote: > >> > >>> Oh, so you want to WRITE a file *like* that. > >>> Use ?sprintf. > >>> (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8) > >>> > >>> Fortran sprintf > >>> 1X a space > >>> 3X 3 spaces > >>> F13.5 %13.5f > >>> F8.2%8.2f > >>> A5 %5s > >>> A8 %8s > >>> I8 %8d > >>> 3(...) the translation of ... written 3 times > >>> > >>> We can simplify 1X,F13.5 to F14.5 > >>> Here's the sprintf() equivalent of the Fortran format, > >>> except you'll need to change the dots to spaces. > >>> > >>> "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n > >>> > >>> On Thu, 22 Sept 2022 at 04:17, javad bayat > 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, 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,
Re: [R] Write text file in Fortran format
Hello, Are those lines at the begining of the file? Rui Barradas Às 06:44 de 22/09/2022, javad bayat escreveu: Dear all; Many thanks for your useful comments and codes. I tried both Rui's and Jim's codes. Jim's codes gave an error as below: "Error in substr(inputline, 1, begincol3 - 1) : object 'inputline' not found" I don't know what's wrong. The Rui's codes worked correctly for the attached file. But I have edited that file and removed 2 last lines from the file because I could not read it into R. These 2 lines were: " ** CONCUNIT ug/m^3 ** DEPUNIT g/m^2 " When I tried to run the code for my file that has these 2 lines at the end, it gave me this error: " df1 <- read.table(text = txt_table) Error in read.table(text = txt_table) : no lines available in input " The codes before the "df1 <- read.table(text = txt_table)" were run correctly. Sincerely On Thu, Sep 22, 2022 at 6:58 AM javad bayat wrote: Dear all; I apologise, I didn't know that I have to cc the list. Thank you Mr Rui for reminding me. Let me clarify more. 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. Maybe I have removed the last 2 lines, but at the end it had 2 lines similar the first 8 lines which starts with asterisk. 2- Double the 3rd column values (or multiply by specific number). 3- Insert the removed first 8 lines of the original text file as header in the dataframe. 4- Insert the removed last 2 lines 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. I will try all your codes in a few hours, cause I am out of office. Sincerely yours. On Thu, 22 Sep 2022, 04:20 Richard O'Keefe, wrote: Oh, so you want to WRITE a file *like* that. Use ?sprintf. (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8) Fortran sprintf 1X a space 3X 3 spaces F13.5 %13.5f F8.2%8.2f A5 %5s A8 %8s I8 %8d 3(...) the translation of ... written 3 times We can simplify 1X,F13.5 to F14.5 Here's the sprintf() equivalent of the Fortran format, except you'll need to change the dots to spaces. "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n On Thu, 22 Sept 2022 at 04:17, javad bayat 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, 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@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]] __
Re: [R] Write text file in Fortran format
Dear all; Many thanks for your useful comments and codes. I tried both Rui's and Jim's codes. Jim's codes gave an error as below: "Error in substr(inputline, 1, begincol3 - 1) : object 'inputline' not found" I don't know what's wrong. The Rui's codes worked correctly for the attached file. But I have edited that file and removed 2 last lines from the file because I could not read it into R. These 2 lines were: " ** CONCUNIT ug/m^3 ** DEPUNIT g/m^2 " When I tried to run the code for my file that has these 2 lines at the end, it gave me this error: " > df1 <- read.table(text = txt_table) Error in read.table(text = txt_table) : no lines available in input " The codes before the "df1 <- read.table(text = txt_table)" were run correctly. Sincerely On Thu, Sep 22, 2022 at 6:58 AM javad bayat wrote: > Dear all; > I apologise, I didn't know that I have to cc the list. > Thank you Mr Rui for reminding me. > Let me clarify more. > 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. > Maybe I have removed the last 2 lines, but at the end it had 2 lines > similar the first 8 lines which starts with asterisk. > 2- Double the 3rd column values (or multiply by specific number). > 3- Insert the removed first 8 lines of the original text file as header in > the dataframe. > 4- Insert the removed last 2 lines 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. > I will try all your codes in a few hours, cause I am out of office. > Sincerely yours. > > > > On Thu, 22 Sep 2022, 04:20 Richard O'Keefe, wrote: > >> Oh, so you want to WRITE a file *like* that. >> Use ?sprintf. >> (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8) >> >> Fortran sprintf >> 1X a space >> 3X 3 spaces >> F13.5 %13.5f >> F8.2%8.2f >> A5 %5s >> A8 %8s >> I8 %8d >> 3(...) the translation of ... written 3 times >> >> We can simplify 1X,F13.5 to F14.5 >> Here's the sprintf() equivalent of the Fortran format, >> except you'll need to change the dots to spaces. >> >> "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n >> >> On Thu, 22 Sept 2022 at 04:17, javad bayat 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, 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> >
Re: [R] Write text file in Fortran format
Dear all; I apologise, I didn't know that I have to cc the list. Thank you Mr Rui for reminding me. Let me clarify more. 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. Maybe I have removed the last 2 lines, but at the end it had 2 lines similar the first 8 lines which starts with asterisk. 2- Double the 3rd column values (or multiply by specific number). 3- Insert the removed first 8 lines of the original text file as header in the dataframe. 4- Insert the removed last 2 lines 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. I will try all your codes in a few hours, cause I am out of office. Sincerely yours. On Thu, 22 Sep 2022, 04:20 Richard O'Keefe, wrote: > Oh, so you want to WRITE a file *like* that. > Use ?sprintf. > (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8) > > Fortran sprintf > 1X a space > 3X 3 spaces > F13.5 %13.5f > F8.2%8.2f > A5 %5s > A8 %8s > I8 %8d > 3(...) the translation of ... written 3 times > > We can simplify 1X,F13.5 to F14.5 > Here's the sprintf() equivalent of the Fortran format, > except you'll need to change the dots to spaces. > > "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n > > On Thu, 22 Sept 2022 at 04:17, javad bayat 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, 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@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@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@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.
Re: [R] Write text file in Fortran format
Background: there is a data file whose records, after a header, can be describedby the Fortran format given in the header. YES, you can easily read that file in R, and you don't even need to know anything about Fortran formats to do it. You can read the file as a data frame using read.table using the skip=8 parameter to skip the header and the colnames=c("x", "y", ...) parameter to supply the column names. You will need to supply the column names yourself because one of the column names given has a space in it. On Wed, 21 Sept 2022 at 20:29, javad bayat wrote: > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Write text file in Fortran format
Sorry, line 9 should be: if(substr(air[airline],1,1) == " ") Jim On Thu, Sep 22, 2022 at 8:51 AM Jim Lemon wrote: > > Hi Javad, > The example text file you sent doesn't appear to have the two > "trailer" lines that you describe. I would try this brute force method > as all you seem to want is to multiply the third column values by a > constant: > > air<-readLines("Air.txt") > # define the column 3 field > begincol3<-29 > endcol3<-42 > widthcol3<-14 > lenline<-117 > for(airline in 1:length(air)) { > # is this a data line? > if(substr(air[airline]) == " ") > air[airline]<-paste0(substr(inputline,1,begincol3-1), > formatC(as.double(substr(inputline,begincol3,endcol3))*mult3, >digits=5,width=widthcol3,format="f"), > substr(inputline,endcol3+1,lenline)) > } > writeLines(air,"Air2.txt") > > Jim > > On Wed, Sep 21, 2022 at 9:25 PM javad bayat wrote: > > > > Dear Jim; > > I have no knowledge of the FORTRAN language. The text file 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 show 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 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. > > Is it possible? > > Sincerely > > > > > > > > > > > > > > On Wed, Sep 21, 2022 at 3:29 PM Jim Lemon wrote: > >> > >> Hi, > >> There was a .txt attachment that is some type of data file. Maybe what > >> you mean is can the header be stripped and the rest written out in a > >> format that can be input with a FORTRAN read routine. Yes, you just > >> have to read it into a data frame and write it out so that it is in > >> either free or fixed format. You will have to define the variable > >> names in your FORTRAN code and work out the input format > >> specifications for the fields. > >> > >> Jim > >> > >> On Wed, Sep 21, 2022 at 8:30 PM Ebert,Timothy Aaron wrote: > >> > > >> > This post was empty. > >> > > >> > Tim > >> > > >> > From: R-help On Behalf Of javad bayat > >> > Sent: Wednesday, September 21, 2022 2:09 AM > >> > To: R-help@r-project.org > >> > Subject: [R] Write text file in Fortran format > >> > > >> > > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > __ > >> > R-help@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. > > > > > > > > -- > > Best Regards > > Javad Bayat > > M.Sc. Environment Engineering > > Alternative Mail: bayat...@yahoo.com __ R-help@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.
Re: [R] Write text file in Fortran format
Hi Javad, The example text file you sent doesn't appear to have the two "trailer" lines that you describe. I would try this brute force method as all you seem to want is to multiply the third column values by a constant: air<-readLines("Air.txt") # define the column 3 field begincol3<-29 endcol3<-42 widthcol3<-14 lenline<-117 for(airline in 1:length(air)) { # is this a data line? if(substr(air[airline]) == " ") air[airline]<-paste0(substr(inputline,1,begincol3-1), formatC(as.double(substr(inputline,begincol3,endcol3))*mult3, digits=5,width=widthcol3,format="f"), substr(inputline,endcol3+1,lenline)) } writeLines(air,"Air2.txt") Jim On Wed, Sep 21, 2022 at 9:25 PM javad bayat wrote: > > Dear Jim; > I have no knowledge of the FORTRAN language. The text file 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 show 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 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. > Is it possible? > Sincerely > > > > > > > On Wed, Sep 21, 2022 at 3:29 PM Jim Lemon wrote: >> >> Hi, >> There was a .txt attachment that is some type of data file. Maybe what >> you mean is can the header be stripped and the rest written out in a >> format that can be input with a FORTRAN read routine. Yes, you just >> have to read it into a data frame and write it out so that it is in >> either free or fixed format. You will have to define the variable >> names in your FORTRAN code and work out the input format >> specifications for the fields. >> >> Jim >> >> On Wed, Sep 21, 2022 at 8:30 PM Ebert,Timothy Aaron wrote: >> > >> > This post was empty. >> > >> > Tim >> > >> > From: R-help On Behalf Of javad bayat >> > Sent: Wednesday, September 21, 2022 2:09 AM >> > To: R-help@r-project.org >> > Subject: [R] Write text file in Fortran format >> > >> > >> > >> > [[alternative HTML version deleted]] >> > >> > __ >> > R-help@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. > > > > -- > Best Regards > Javad Bayat > M.Sc. Environment Engineering > Alternative Mail: bayat...@yahoo.com __ R-help@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.
Re: [R] Write text file in Fortran format
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 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, 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@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@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@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.
Re: [R] Write text file in Fortran format
Hello, You forgot to cc the list. Here is a solution, I believe the comments explain the several steps. It's a matter to read in the file, separate the table from the rest, update the 3rd column, assemble the pieces back together and write to file. # this path depends on the OP's system path <- "~/Temp" fl <- list.files(path, pattern = "Air.txt", full.names = TRUE) # read the file as text txt <- readLines(fl) # find lines starting with an asterisk i <- grep("^\\*", txt) # keep the other lines txt_table <- txt[-i] # find the lines starting or after the first blank line j <- which(seq_along(txt_table) >= which(txt_table == "")) # keep the other lines txt_table <- txt_table[-j] txt_table <- trimws(txt_table) df1 <- read.table(text = txt_table) # multiply 3rd column by 2 df1[[3]] <- 2*df1[[3]] # format the table's lines to write them back to file fmt <- "%14.5f %13.5f %13.5f %8.2f %8.2f %8.2f %7s %8s %10s %9s %9d" new_data <- sprintf(fmt, df1[[1]], df1[[2]], df1[[3]], df1[[4]], df1[[5]], df1[[6]], df1[[7]], df1[[8]], df1[[9]], df1[[10]], df1[[11]]) j <- which(seq_along(txt) >= which(txt == "")) # assemble the header, the table and the footer txt_new <- c(txt[i], new_data, txt[j]) # this is not in the question, # I'm not rewriting the original fl2 <- file.path(dirname(fl), "Air2.txt") writeLines(txt_new, fl2) Hope this helps, rui Barradas Às 15:55 de 21/09/2022, javad bayat escreveu: Dear Rui; 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, 16:46 Rui Barradas, wrote: Hello, The attached file ends with R-Help's end message. This is unrelated to computer languages, Fortran, R or other. And there is no such thing as a Fortran format. Can you please describe the problem you have? Rui Barradas Às 07:09 de 21/09/2022, javad bayat escreveu: __ R-help@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.
Re: [R] Write text file in Fortran format
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, 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@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@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.
Re: [R] Write text file in Fortran format
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@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.
Re: [R] Write text file in Fortran format
Hello, The attached file ends with R-Help's end message. This is unrelated to computer languages, Fortran, R or other. And there is no such thing as a Fortran format. Can you please describe the problem you have? Rui Barradas Às 07:09 de 21/09/2022, javad bayat escreveu: __ R-help@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.
Re: [R] Write text file in Fortran format
Hi, There was a .txt attachment that is some type of data file. Maybe what you mean is can the header be stripped and the rest written out in a format that can be input with a FORTRAN read routine. Yes, you just have to read it into a data frame and write it out so that it is in either free or fixed format. You will have to define the variable names in your FORTRAN code and work out the input format specifications for the fields. Jim On Wed, Sep 21, 2022 at 8:30 PM Ebert,Timothy Aaron wrote: > > This post was empty. > > Tim > > From: R-help On Behalf Of javad bayat > Sent: Wednesday, September 21, 2022 2:09 AM > To: R-help@r-project.org > Subject: [R] Write text file in Fortran format > > > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
Re: [R] Write text file in Fortran format
This post was empty. Tim From: R-help On Behalf Of javad bayat Sent: Wednesday, September 21, 2022 2:09 AM To: R-help@r-project.org Subject: [R] Write text file in Fortran format [[alternative HTML version deleted]] __ R-help@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.