Dear R users,[IF THE FORMAT OF MY EMAIL IS NOT CLEAR, I HAVE ATTACHED A TEXT FILE FOR A CLEAR VIEW] I would like to use the R output file in Fortran. my file Is exactly in the following format. ELISA/BOTTO wATER INN FROM 1900 11 1 TO 1996 12 31 1901.11. 1 447.000 1901.11. 2 445.000 1901.11. 3 445.000 1924.11. 4 445.000 1924.11. 5 449.000 1924.11. 6 442.000 1924.11. 7 445.000 so you can see that there is a single space, in between these lines and also a single space from left margin. i would like to keep the left margin space, but really like to eliminate the space between the lines so that i get an output text file which could look like ELISA/BOTTO wATER INN FROM 1900 11 1 TO 1996 12 31 1901.11. 1 447.000 1901.11. 2 445.000 1901.11. 3 445.000 1924.11. 4 445.000 1924.11. 5 449.000 1924.11. 6 442.000 1924.11. 7 445.000 As i am working in fortran i would really like to keep the said format.THANKS IN ADVANCE ELISA -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: des.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130211/fe8d6ff6/attachment.txt>
Hi Elisa, It seems also that there are spaces between the second '.' and the number (1901.11. 1). You could do something like: Lines1<-readLines(textConnection("1901.11. 1 447.000 ? ?1901.11. 2 445.000 ? ?1901.11. 3 445.000 ? ?1924.11. 4 445.000 ? ?1924.11. 5 449.000 ? ?1924.11. 6 442.000 ? ?1924.11. 7 445.000 ")) ?gsub("(.*..*.)\\s+(.*\\s+.*)","\\1\\2",gsub("^ ","",Lines1[Lines1!=""])) #[1] "1901.11.1 447.000" "1901.11.2 445.000" "1901.11.3 445.000" #[4] "1924.11.4 445.000" "1924.11.5 449.000" "1924.11.6 442.000" #[7] "1924.11.7 445.000" read.table(text=gsub("(.*..*.)\\s+(.*\\s+.*)","\\1\\2",gsub("^ ","",Lines1[Lines1!=""])),sep="",header=FALSE,stringsAsFactors=FALSE) ?# ?????? V1? V2 #1 1901.11.1 447 #2 1901.11.2 445 #3 1901.11.3 445 #4 1924.11.4 445 #5 1924.11.5 449 #6 1924.11.6 442 #7 1924.11.7 445 #Using read.table directly also removes the spaces dat1<-read.table(text=" 1901.11. 1 447.000 ? ?1901.11. 2 445.000 ? ?1901.11. 3 445.000 ? ?1924.11. 4 445.000 ? ?1924.11. 5 449.000 ? ?1924.11. 6 442.000 ? ?1924.11. 7 445.000 ",sep="",header=FALSE,stringsAsFactors=FALSE) ?dat1 #??????? V1 V2? V3 #1 1901.11.? 1 447 #2 1901.11.? 2 445 #3 1901.11.? 3 445 #4 1924.11.? 4 445 #5 1924.11.? 5 449 #6 1924.11.? 6 442 #7 1924.11.? 7 445 ----- Original Message ----- From: eliza botto <eliza_botto at hotmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, February 11, 2013 11:41 AM Subject: [R] FORMAT EDITING Dear R users,[IF THE FORMAT OF MY EMAIL IS NOT CLEAR, I HAVE ATTACHED A TEXT FILE FOR A CLEAR VIEW] I would like to use the R output file in Fortran. my file Is exactly in the following format. ELISA/BOTTO? wATER INN? ? ? ? ? ? ? FROM 1900 11 1 TO 1996 12 31 1901.11. 1 447.000 1901.11. 2 445.000 1901.11. 3 445.000 1924.11. 4 445.000 1924.11. 5 449.000 1924.11. 6 442.000 1924.11. 7 445.000 so you can see that there is a single space, in between these lines and also a single space from left margin. i would like to keep the left margin space, but really like to eliminate the space between the lines so that i get an output text file which could look like ELISA/BOTTO? wATER INN? ? ? ? ? ? ? FROM 1900 11 1 TO 1996 12 31 1901.11. 1 447.000 1901.11. 2 445.000 1901.11. 3 445.000 1924.11. 4 445.000 1924.11. 5 449.000 1924.11. 6 442.000 1924.11. 7 445.000 As i am working in fortran i would really like to keep the said format.THANKS IN ADVANCE ELISA ??? ??? ??? ? ??? ??? ? ______________________________________________ R-help at r-project.org mailing list 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.
You say, "? use the R output file in Fortran ? " I guess that means that R is writing an output file which will then be used as an input file for a fortran program. In that case, you need to go back to how R is writing the output file, find out why it is writing blank lines, and correct it. As far as leading spaces and other formatting aspects, you can create any output format you want using functions like cat(), formatC(), sprintf() and others. Otherwise, if what you want R to do is read the input file, remove the empty lines, and write a new output file, this seems to do it (on my system): tmp <- scan('des.txt', what='', sep='\n') cat( paste(tmp,collapse='\n') , file='des.new') Because as far as I can tell from your attached text file, the input and output are identical except for the blank lines. You might also be dealing with issues of different operating systems having different conventions for new lines (DOS vs unix, etc). emacs tells me you input data is DOS, meaning new lines are indicated (if I remember correctly) by a sequence of two characters, "carriage return" followed by "line feed", but the fortran code may be expecting a single character. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 2/11/13 8:41 AM, "eliza botto" <eliza_botto at hotmail.com> wrote:> > >Dear R users,[IF THE FORMAT OF MY EMAIL IS NOT CLEAR, I HAVE ATTACHED A >TEXT FILE FOR A CLEAR VIEW] >I would like to use the R output file in Fortran. my file Is exactly in >the following format. > ELISA/BOTTO wATER INN > FROM 1900 11 1 TO 1996 12 31 > 1901.11. 1 447.000 > 1901.11. 2 445.000 > 1901.11. 3 445.000 > 1924.11. 4 445.000 > 1924.11. 5 449.000 > 1924.11. 6 442.000 > 1924.11. 7 445.000 >so you can see that there is a single space, in between these lines and >also a single space from left margin. i would like to >keep the left margin space, but really like to eliminate the space >between the lines so that i get an output text file which >could look like > ELISA/BOTTO wATER INN FROM 1900 11 1 TO 1996 12 31 >1901.11. 1 447.000 1901.11. 2 445.000 1901.11. 3 445.000 1924.11. 4 >445.000 1924.11. 5 449.000 1924.11. 6 442.000 1924.11. 7 445.000 >As i am working in fortran i would really like to keep the said >format.THANKS IN ADVANCE >ELISA