how to read many files with different skip lines at one time

19 views
Skip to first unread message

Tian

unread,
Feb 23, 2015, 10:15:51 AM2/23/15
to ggp...@googlegroups.com
I have many txt files and its style is shown  below:

%File
FileName=D0005234.204
FileDate=19-Feb-2015
FileTime=20:08:44
sample name=xxx1
temperature= 25
%Comment
100mm
%Counts
 1.196e-02, 7.195e-01, 2.506e-02,  0.000e+00, 0
 1.461e-02, 7.220e-01, 1.488e-02,  0.000e+00, 0
 1.727e-02, 7.987e-01, 1.332e-02,  0.000e+00, 0
 1.992e-02, 8.288e-01, 1.168e-02,  0.000e+00, 0
.................
................
.................


I want to read these files with R at one time. The codes are:

     extension <- "txt"
     fileNames <- Sys.glob(paste("*.", extension, sep = ""))
     fileNumbers <- seq(fileNames)
     for (fileNumber in fileNumbers) {
     mydata <- read.table(fileNames[fileNumber],skip=11,sep=",")

However, these files have different skip lines, such as skip=8, skip=12,........... It means that the frame data begin from different line. How to solve this problem?
It brings me a lot of troubles to check each file.

Thank you!

Ben Bond-Lamberty

unread,
Feb 23, 2015, 10:30:42 AM2/23/15
to ggplot2
This isn't a ggplot question, so please note that you're posting to
the wrong listserv. However, I would use readLines() to read the file
into a temporary variable. Then use grep to find that "%Counts" line
(assuming that it always prefixes the data lines you want). For
example, something like this:

temp <- readLines(fileNames[fileNumber], n=50) # assuming no more
than 50 header lines
lastHeaderRow <- which(grepl(temp, "%Count", fixed=TRUE)
mydata <- read.table(fileNames[fileNumber], skip=lastHeaderRow, sep=",")

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages