Tom Cohen
2007-Aug-02 21:14 UTC
[R] problem with reading data files with different numbers of lines to skips
Dear List, I have 30 data files with different numbers of lines (31 and 33) that I want to skip before reading the files. If I use the skip option I can only choose either to skip 31 or 33 lines. The data files with 31 lines have no blank rows between the lines and the header row. How can I read the files without manually checking which files have 31 respectively 33 lines ? The only text line I want to keep is the header. Thamks for your help, Tom for (i in 1:num.files) { a<-read.table(file=data[i], ,header=T,skip=31,sep='\t',na.strings="NA") } --------------------------------- Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling. [[alternative HTML version deleted]]
Ross Darnell
2007-Aug-02 22:00 UTC
[R] problem with reading data files with different numbers of lines to skips
You could read the file and test each line (try readline()) but you need some rule to distinguish between text lines and data lines. You could then reread the file with the number of skip lines defined. Ross Darnell -----Original Message----- From: r-help-bounces@stat.math.ethz.ch on behalf of Tom Cohen Sent: Fri 03-Aug-07 7:14 AM To: r-help@stat.math.ethz.ch Subject: [R] problem with reading data files with different numbers of lines to skips Dear List, I have 30 data files with different numbers of lines (31 and 33) that I want to skip before reading the files. If I use the skip option I can only choose either to skip 31 or 33 lines. The data files with 31 lines have no blank rows between the lines and the header row. How can I read the files without manually checking which files have 31 respectively 33 lines ? The only text line I want to keep is the header. Thamks for your help, Tom for (i in 1:num.files) { a<-read.table(file=data[i], ,header=T,skip=31,sep='\t',na.strings="NA") } --------------------------------- Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling. [[alternative HTML version deleted]] [[alternative HTML version deleted]]
Gabor Grothendieck
2007-Aug-02 22:30 UTC
[R] problem with reading data files with different numbers of lines to skips
See: http://tolstoy.newcastle.edu.au/R/help/03b/6094.html On 8/2/07, Tom Cohen <tom.cohen78 at yahoo.se> wrote:> Dear List, > > I have 30 data files with different numbers of lines (31 and 33) that I want to skip before reading the files. If I use the skip option I can only choose either to skip 31 or 33 lines. The data files with 31 lines have no blank rows between the lines and the header row. How can I read the files without manually checking which files have 31 respectively 33 lines ? The only text line I want to keep is the header. > > Thamks for your help, > Tom > > > for (i in 1:num.files) { > a<-read.table(file=data[i], > ,header=T,skip=31,sep='\t',na.strings="NA") > > } > > > > > > --------------------------------- > G?r det l?ngsamt? Skaffa dig en snabbare bredbandsuppkoppling. > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >
(Ted Harding)
2007-Aug-02 22:52 UTC
[R] problem with reading data files with different numbers o
On 02-Aug-07 21:14:20, Tom Cohen wrote:> Dear List, > > I have 30 data files with different numbers of lines (31 and 33) that > I want to skip before reading the files. If I use the skip option I can > only choose either to skip 31 or 33 lines. The data files with 31 lines > have no blank rows between the lines and the header row. How can I read > the files without manually checking which files have 31 respectively 33 > lines ? The only text line I want to keep is the header. > > Thamks for your help, > Tom > > > for (i in 1:num.files) { > a<-read.table(file=data[i], > ,header=T,skip=31,sep='\t',na.strings="NA") > > }If you're using a Unix/Linux system, you have the little command "wc" which can count characters or words or lines in a file. For example, I'm working at the moment on a file "mkSim.R" which has 53 lines. So, in R:> system("wc -l mkSim.R")53 mkSim.R Hence the following returns the line-count as an integer:> as.integer(substring(system("wc -l mkSim.R",intern=TRUE),1,7))[1] 53 (which will also work for files with hundreds, thousands, ... of lines, since the units digit is at position 7). Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 02-Aug-07 Time: 23:51:59 ------------------------------ XFMail ------------------------------
(Ted Harding)
2007-Aug-02 23:11 UTC
[R] problem with reading data files with different numbers o
On 02-Aug-07 21:14:20, Tom Cohen wrote:> Dear List, > > I have 30 data files with different numbers of lines (31 and 33) that > I want to skip before reading the files. If I use the skip option I can > only choose either to skip 31 or 33 lines. The data files with 31 lines > have no blank rows between the lines and the header row. How can I read > the files without manually checking which files have 31 respectively 33 > lines ? The only text line I want to keep is the header. > > Thamks for your help, > Tom > > > for (i in 1:num.files) { > a<-read.table(file=data[i], > ,header=T,skip=31,sep='\t',na.strings="NA") > > }Apologies, I misunderstood your description in my previous response (I thought that the total number of lines in one of your files was either 31 or 33, and you wanted to know which was which). I now think you mean that there are either 0 (you want to skip 31) or 2 (you want to skip 33) blank lines in the first 33, and then you want the remainder (aswell as the header). Though it's still not really clear ... You can find out how many blank lines there are in the first 33 with> sum(cbind(readLines("~/00_junk/temp.tr", 33))=="")and then choose how many lines to skip. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Aug-07 Time: 00:11:21 ------------------------------ XFMail ------------------------------