[R] Dataimport with readLines using skip= and nlines= ?

Felix Wave felix-wave at vr-web.de
Mon Apr 16 15:02:02 CEST 2007


Hello,
I have a problem with readLines.
I have a data file with many informations added with a different number of measurments (example at the end).
I only want to read the measurments witch start with "START OF HEIGHT DATA" and end with "END OF HEIGHT DATA". 

The difficulty is: 
-I want to read the file with "readLines", because the measurments have letters and 
numbers (e.g. S=0.00012) witch have to be filtered for using a matrix (scan make errors). 
-The second problem is, that the number of measurments is variable.

My idea. I have the position (skip=r2) of the beginning of the measurments, the length (nlines=r1) and the number of all measurments (for (i in i:r3)).

My problem and question. I need a command to read my measurments.
The readLine command has no possibility to skip lines. And the read.file command
can't filter my data.dat for (S= , S=, x=, y=).

I hope you have an idea.
Thank's for all.
Felix



My R Code:
--------------

#################################################
#R Code to find to measurments in the data file #
#################################################
location <- my.read.file( "dat.dat" )  
    
my.read.file <- function(file=file){                               
a1 <- readLines( con=file, n=-1 )                                    
a2 <- grep("START OF HEIGHT DATA|END OF HEIGHT DATA", a1 )          
a3 <- matrix(a2, ncol=2)              #matrix with the location of START & END                                          
}   
                                                                
r1 <- location[,2] - location[,1]    #distance between START & END                                 
r2 <- location[,1]                         #position of START
r3 <- length(location) / 2             #number of START & ENDs 


################################################
# R Code to read the measurments ###############
################################################
for (i in 1:r3)

Measure[i] <- meas.read.file( "dat.dat" )                    #single matrix for every measurment

meas.read.file <-  function(file=file){                           #HERE IS MY PROBLEM                         
#v1 <- read.table("dat.dat", skip=r2[i], nlines=r3[i])  #HERE IS MY PROBLEM  
v2 <- readLines( con=file, n=-1)                                         
v3 <- gsub(" ", "", v2)                                                   
v4 <- gsub( "S=|y=|x=", " ", v3 )                                         
v5 <- gsub("^ ", "", v4)                                                  
                                                                          
m <- t( sapply( strsplit(v5, split=" "), as.numeric ) )                   
colnames(m) <- c("S", "y", "x" )                                          
return(m)                                                                 
}            




My Datafile:
########                                                        


EXAM NUM:2
-----------------
EXAM #1
ASTIG:-2.4D
AXIS:4.8
START OF HEIGHT DATA
S= 0 y=0.0 x=0.00000000
S= 0 y=0.1 x=0.00055643
S= 9 y=4.9 x=1.67278117
S= 9 y=5.0 x=1.74873257
S=10 y=0.0 x=0.00000000
S=10 y=0.1 x=0.00075557
S=99 y=5.3 x=1.94719490
END OF HEIGHT DATA
X POS:-0.299mm
Y POS:0.442mm
Z POS:-0.290mm
-----------------
EXAM #2
ASTIG:-2.4D
AXIS:4.8
START OF HEIGHT DATA
S= 0 y=0.0 x=0.00000000
S= 0 y=0.1 x=0.00055643
S= 9 y=4.9 x=1.67278117
S= 9 y=5.0 x=1.74873257
S=10 y=0.0 x=0.00000000
S=10 y=0.1 x=0.00075557
S=99 y=5.3 x=1.94719490
END OF HEIGHT DATA
X POS:-0.299mm
Y POS:0.442mm
Z POS:-0.290mm



More information about the R-help mailing list