[R] read tab delimited file from a certain line

David Winsemius dwinsemius at comcast.net
Fri Jan 18 21:11:48 CET 2013


On Jan 18, 2013, at 10:26 AM, Henrik Bengtsson wrote:

> Christof,
> 
> I've added support for this to the R.filesets package.  In your case,
> then all you need to do is:
> 
> library("R.filesets")
> dlf <- readDataFrame(filename, skip="^year")
> 
> No need to specify any other arguments - they're all automagically
> inferred - and the default is stringsAsFactors=FALSE.
> 
> This is in R.filesets v2.0.0 which I still haven't published on CRAN.
> In the meanwhile, you can install it via:
> 
> source("http://aroma-project.org/hbLite.R")
> hbLite("R.filesets")

Mac GUI users should exit to a Terminal session running R --vanilla  before attempting this. (Probably a safer practice in general, but sometimes we get get lazy because the GUI Package Installer is so handy.)

-- 
David.
> 
> /Henrik
> 
> On Thu, Jan 17, 2013 at 1:34 AM, Christof Kluß <ckluss at email.uni-kiel.de> wrote:
>> 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.
>>> 
>>> 
>> 
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list