[R] write.table equivalent for lists?

William Dunlap wdunlap at tibco.com
Mon Jan 10 22:28:33 CET 2011


serialize() and unserialize() in the base package
should do the job also and are usually quite a bit
faster than dput() and dget().  However, the format
is not easily read by other programs.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Greg Snow
> Sent: Monday, January 10, 2011 12:59 PM
> To: Rainer M Krug; R-help
> Subject: Re: [R] write.table equivalent for lists?
> 
> How about dput and dget in the base package?
> 
> -- 
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
> 
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Rainer M Krug
> > Sent: Monday, January 10, 2011 7:24 AM
> > To: R-help
> > Subject: [R] write.table equivalent for lists?
> > 
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> > 
> > Hi
> > 
> > I am writing simulations in R, and quite regularly, I have to save
> > lists
> > and objects to HDD and load it later again.
> > 
> > So I am wondering: why is there no function to write lists 
> (and S3, S4
> > objects) onto HDD WITHOUT keeping the name? What I mean is:
> > 
> > For data.frames I can use
> > 
> > x <- data.frame(x = runif(10))
> > write.table(x, "x.txt)
> > rm(x)
> > 
> > y <- read.table("x.txt")
> > 
> > to load it into y.
> > 
> > But for lists and S3, S4 objects, I have to use save() and load():
> > 
> > x <- list(x=1:10, y=letters(1:10))
> > save(x, file="x.rdata")
> > rm(x)
> > 
> > load("x.rdata") #1
> > y <- x          #2
> > rm(x)           #3
> > 
> > to load it into y.
> > I don't mind the binary of save - what I am really missing 
> is a command
> > which combines lines #1, #2 and #3.
> > 
> > I know - I could write my own
> > 
> > save.list <- function(x, file) {
> >   save(s, file=file)
> > }
> > 
> > load.list <- function(file) {
> >   load(file)
> >   return(x)
> > }
> > 
> > which I could then use as:
> > 
> > x <- list(x=1:10, y=letters(1:10))
> > save.list(x, "test.rdata")
> > 
> > rm(x)
> > 
> > y <- load.list("test.rdata")
> > 
> > but: why is there not such a function? Am I missing something here?
> > 
> > Cheers,
> > 
> > Rainer
> > 
> > - --
> > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> > Biology, UCT), Dipl. Phys. (Germany)
> > 
> > Centre of Excellence for Invasion Biology
> > Natural Sciences Building
> > Office Suite 2039
> > Stellenbosch University
> > Main Campus, Merriman Avenue
> > Stellenbosch
> > South Africa
> > 
> > Tel:        +33 - (0)9 53 10 27 44
> > Cell:       +27 - (0)8 39 47 90 42
> > Fax (SA):   +27 - (0)8 65 16 27 82
> > Fax (D) :   +49 - (0)3 21 21 25 22 44
> > Fax (FR):   +33 - (0)9 58 10 27 44
> > email:      Rainer at krugs.de
> > 
> > Skype:      RMkrug
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v1.4.10 (GNU/Linux)
> > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> > 
> > iEYEARECAAYFAk0rFocACgkQoYgNqgF2egoLMgCfQmZFnLx4Olkb55DtLKmuRXYY
> > yvoAnieTY3/CsGM9pqyoownLxgeuaDrd
> > =VOFu
> > -----END PGP SIGNATURE-----
> > 
> > ______________________________________________
> > 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.
> 



More information about the R-help mailing list