[R] Dataimport with readLines using skip= and nlines= ?
jim holtman
jholtman at gmail.com
Mon Apr 16 16:36:55 CEST 2007
Will this work for you?
> x.in <- "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
+ "
> # read in the lines and delete all except those containing "S="
> input <- readLines(textConnection(x.in))
> input <- input[grep("S=", input)]
> # delete 'Sxy='
> input <- gsub(".=", "", input)
> # read in the data using scan
> myData <- scan(textConnection(input), what=0)
Read 42 items
> matrix(myData, ncol=3, byrow=TRUE, dimnames=list(NULL, c("S", "y", "x")))
S y x
[1,] 0 0.0 0.00000000
[2,] 0 0.1 0.00055643
[3,] 9 4.9 1.67278117
[4,] 9 5.0 1.74873257
[5,] 10 0.0 0.00000000
[6,] 10 0.1 0.00075557
[7,] 99 5.3 1.94719490
[8,] 0 0.0 0.00000000
[9,] 0 0.1 0.00055643
[10,] 9 4.9 1.67278117
[11,] 9 5.0 1.74873257
[12,] 10 0.0 0.00000000
[13,] 10 0.1 0.00075557
[14,] 99 5.3 1.94719490
>
On 4/16/07, Felix Wave <felix-wave at vr-web.de> wrote:
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list