[R] FORMAT EDITING

arun smartpink111 at yahoo.com
Mon Feb 11 18:57:15 CET 2013


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.




More information about the R-help mailing list