[R] formatting a list

Charles C. Berry cberry at tajo.ucsd.edu
Sat Oct 20 23:44:26 CEST 2007


On Sat, 20 Oct 2007, Tomas Vaisar wrote:

> Hi,
>
> I am new to R and need to read in a file with 19 columns and 7000 rows
> and make it into a list of 7000 lists with 19 items each.  For a
> simpler case of 10 by 10 table I used x <-scan("file",
> list(0,0,0,0,0,0,0,0,0,0)), perhaps clumsy, but it did the job.
> However with the large 19x7000 (which needs to be transposed) I am not
> sure how to go about it.
>
> Coudl somebody suggest a way?


 	dat <- as.data.frame( matrix( scan('tmp.txt'), nr=19) )

returns a data.frame which is a list with some added attributes.

or

 	dat <- lapply( readLines('tmp.txt'), function(x) scan(textConnection(x) ) )

Or

 	char.dat <- strsplit( readLines('tmp.txt'), split='[[:blank:]]+')

 	numeric.dat <- lapply( char.dat, as.numeric )

HTH,

Chuck

>
> Thanks,
>
> Tomas
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list