Re: [R] A. Mani : Read

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 24 Jun 2005 - 11:43:10 EST

On 6/23/05, A. Mani <a_mani_sc_gs@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)



R-help@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 Received on Fri Jun 24 11:50:04 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:02 EST