Hi I would like to read table data from a text-files with extra informations in the header (of unknown line count). Example: informations (unknown count of lines) ... and at some point the table ------ year month mday value 2013 1 16 0 ... If it was an excel file I could use something like read.xls(..., pattern="year") But it is a simple tab seperated text-file. Is there an easy way to read only the table? (Without dirty things ;)) Thx Christof
Hello, Read the file using readLines, then grep "^year". You can then use a textConnection to read.table: x <- readLines(con = textConnection( "informations (unknown count of lines) ... and at some point the table ------ year month mday value 2013 1 16 0 ")) # This is it i <- grep("^year", x) read.table(textConnection(x[i:length(x)]), header = TRUE) Hope this helps, Rui Barradas Em 16-01-2013 14:17, Christof Klu? escreveu:> Hi > > I would like to read table data from a text-files with extra > informations in the header (of unknown line count). Example: > > informations (unknown count of lines) > ... and at some point the table > ------ > year month mday value > 2013 1 16 0 > ... > > If it was an excel file I could use something like read.xls(..., > pattern="year") But it is a simple tab seperated text-file. Is there > an easy way to read only the table? (Without dirty things ;)) > > Thx > Christof > > ______________________________________________ > 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.
Lines1 <- readLines(con = textConnection( "informations (unknown count of lines) ... and at some point the table ------ year month mday value 2013 1 16 0 ")) ?indx<-seq(match(regmatches(Lines1,regexpr("^year.*",Lines1)),Lines1),length(Lines1)) read.table(text=Lines1[indx],sep="",header=TRUE) #? year month mday value #1 2013???? 1?? 16???? 0 A.K. ----- Original Message ----- From: Christof Klu? <ckluss at email.uni-kiel.de> To: r-help at stat.math.ethz.ch Cc: Sent: Wednesday, January 16, 2013 9:17 AM Subject: [R] read tab delimited file from a certain line Hi I would like to read table data from a text-files with extra informations in the header (of unknown line count). Example: informations (unknown count of lines) ... and at some point the table ------ year month mday value 2013 1 16 0 ... ??? ??? ??? ??? If it was an excel file I could use something like read.xls(..., pattern="year") But it is a simple tab seperated text-file. Is there an easy way to read only the table? (Without dirty things ;)) Thx Christof ______________________________________________ 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.
Hello thank you for the fast and helpful answer! Now the following works fine for me x <- readLines(filename) i <- grep("^year", x) dlf <- read.table(textConnection(x[i:length(x)]), header = T, stringsAsFactors=F,sep="\t") Greetings Christof Am 16-01-2013 16:55, schrieb Rui Barradas:> Hello, > > Read the file using readLines, then grep "^year". You can then use a > textConnection to read.table: > > x <- readLines(con = textConnection( > "informations (unknown count of lines) > ... and at some point the table > ------ > year month mday value > 2013 1 16 0 ")) > > # This is it > i <- grep("^year", x) > read.table(textConnection(x[i:length(x)]), header = TRUE) > > > Hope this helps, > > Rui Barradas > > Em 16-01-2013 14:17, Christof Klu? escreveu: >> Hi >> >> I would like to read table data from a text-files with extra >> informations in the header (of unknown line count). Example: >> >> informations (unknown count of lines) >> ... and at some point the table >> ------ >> year month mday value >> 2013 1 16 0 >> ... >> >> If it was an excel file I could use something like read.xls(..., >> pattern="year") But it is a simple tab seperated text-file. Is there >> an easy way to read only the table? (Without dirty things ;)) >> >> Thx >> Christof >> >> ______________________________________________ >> 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. >