[R] A. Mani : Read

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 24 03:43:10 CEST 2005


On 6/23/05, A. Mani <a_mani_sc_gs at vsnl.net> wrote:
> Hello,
>       What is the best way to read a .rdat file with data in the following
> form :
> 
> $xyz1
> column names
> columns(numeric)
> 
> $xyz2
> column names
> columns(numeric)
> 
> $xyz3
> column names
> columns(numeric)
> 

RRead in the data using readLines.  starting lines are those
1 after those that start with $ and ending lines are one
before those that are empty.  In case there are multiple
empty lines find the smallest empty line greater than each
starting line using cut.  Now for each data frame use read.table 
to read in line numbers starts[i] through ends[i] into a list
called tables, one element per data frame, and set the names
of tables to the lines beginning with $ (without the $).

Lines <- readLines(myfile)
starts <- grep("^\\$", Lines)  + 1
ends <- grep("^[[:space:]]*$", c(Lines, ""))
ends <- ends[cut(starts, c(0, ends), lab = FALSE)]-1
tables <- lapply(seq(along = starts), function(i) 
	read.table(textConnection(Lines[starts[i]:ends[i]]), header = TRUE))
names(tables) <- substr(Lines[starts-1],2)




More information about the R-help mailing list