[R] Combining many dataframes from listings of objects?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed May 15 11:56:07 CEST 2002


On Wed, 15 May 2002, Derek Eder wrote:

> I want to combine (rbind) many dataframes into a single data frame, but "automatically" specifying the names of the dataframes as listing of object names.
>
> E.g., combine these 18 df objects into one big df using something conceptually like this :
>
> rbind(objects(pattern="*.df"))

A job for do.call.  I think

do.call("rbind", objects(pattern="*.df"))

might work, and something like it should.

>
>  ...
>
> > objects(pattern="*.df")
>  [1] "BA.Nt1.nREM.APNEA.MIXED.bp.df"       "BA.Nt1.nREM.APNEA.OBSTRUCTIVE.bp.df"
>  [3] "BA.Nt1.nREM.HYPOPNEA.bp.df"          "BA.Nt1.REM.HYPOPNEA.bp.df"
>  [5] "BA.Nt2.nREM.APNEA.MIXED.bp.df"       "BA.Nt2.nREM.APNEA.OBSTRUCTIVE.bp.df"
>  [7] "BA.Nt2.nREM.HYPOPNEA.bp.df"          "BA.Nt2.REM.APNEA.OBSTRUCTIVE.bp.df"
>  [9] "BA.Nt2.REM.HYPOPNEA.bp.df"           "BF.Nt1.nREM.APNEA.MIXED.bp.df"
>
>
>
> By the way in S. I used to do this by creating a large empty target dataframe and then
> looping through the list of objects, write each one into the target.  This is failing in R:
>
> > target_as.data.frame(matrix(data=NA,nrow=8,ncol=14))
> > target[1:4,]__c("a","b","c","d")
>
> > Warning messages:
> 1: invalid factor level, NAs generated in: "[<-.factor"(*tmp*, iseq, value = vjj)
> 2: invalid factor level, NAs generated in: "[<-.factor"(*tmp*, iseq, value = vjj)
> 3: invalid factor level, NAs generated in: "[<-.factor"(*tmp*, iseq, value = vjj)
> 4: invalid factor level, NAs generated in: "[<-.factor"(*tmp*, iseq, value = vjj)
> . . .

You need to set the column types up first.

>
>
> Thank you
>
>
> Derek N. Eder
> Göteborgs Universitet
> Institutionen för klinisk neurovetenskap - psykiatri
> Sahlgrenska universitetssjukhuset SS/SU
> Blå straket 17b
> SE 413 45  Göteborg
> Sverige
> Tlf. +46 (031) 34  2-6139  (office)
> Tlf. +46 (031) 34 2-1283  (laboratory)
> Tlf. +46 0709 / 7 2-1283 (mobil)
> Fax. +46 (031) 82 81 63
> derek.eder at neuro.gu.se
>
>
> Gothenburg University
> Institute of Clinical Neuroscience,
> Section for Psychiatry
> Salhgrenska Hospital  SU/SS
> SE 413 45  Göteborg
> Sweden
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list