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 <j.bayat...@gmail.com> 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, <rao...@gmail.com> 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 <j.bayat...@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, <j...@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@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. >>> >> -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat...@yahoo.com [[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.