Combining many dataframes from listings of objects?
Derek Eder asks:
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")) ...
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"
[WNV] This is a natural do.call problem, but you need to be a bit
careful. The second argument has to be a list either of the arguments
themselves or names that evaluate to the arguments. The obvious choice here
is names. The following is a mock example with hints of yours.
> a.df <- b.df <- c.df <- data.frame(x = 1:3, y = letters[1:3])
> a.df
x y
1 1 a
2 2 b
3 3 c
> do.call("rbind", lapply(objects(pat="*.df$"), as.name))
x y
1 1 a
2 2 b
3 3 c
4 1 a
5 2 b
6 3 c
7 1 a
8 2 b
9 3 c
>
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)
. . . 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._