[19136] in s-news-athena

home help back first fref pref prev next nref lref last post

[S] summary: converting a list of dataframes into a single dataframe

daemon@ATHENA.MIT.EDU (John.Gavin@wdr.com)
Wed Sep 8 03:25:42 1999

From: John.Gavin@wdr.com
Date: Wed, 8 Sep 1999 08:19:03 +0100
Message-Id: <H00004ac0a5ddc9a@MHS>
Mime-Version: 1.0
To: s-news@wubios.wustl.edu
Content-Type: text/plain; charset=US-ASCII; name="BDY.TXT"
Content-Disposition: inline; filename="BDY.TXT"
Content-Transfer-Encoding: 7bit


Hi,

Many thanks for the prompt replies.
Solutions, credits and original question follow below.

Regards,

John.

Solutions 
---------
There were two solutions.
The first did exactly what I wanted

1) do.call("rbind",input)

2) output <- NULL
   while(length(input) > 0)
   {
	output <- rbind(output, input[[1]])
	input[[1]] <- NULL
   }

Credits
-------
Bill Venables   Bill.Venables@cmis.csiro.au 
                cberry@tajo.ucsd.edu
Tim Hesterberg  timh@statsci.com
Hong Ooi        hong.ooi@maths.anu.edu.au

Original question
-----------------

I have a long list of data frames.
Each data frame has identical columns 
(same number of columns, in the same order 
and of the same class).
How can I produce a new single data frame formed by 
stacking each data frame in the list one on 
top of the other.

I am aware of the 'data - merge' command but
the list is too long to do two data frames at a time.
In effect, I want to reverse the data split command.

Also each data frame may contain 'factor' and 'ordered' columns
so I want to preserve the original labels.

I am on SPlus 2000 on NT 4.

Example: given the list 'input' I want to produce
the data frame 'output'.

input <- list( 
  a = data.frame(
    x = c(1.0,2,3), 
    y = factor(c(1,1,2), levels = 1:3, labels = c("f1", "f2", "f3") ),
    z = ordered(c(2,2,1), levels = 1:3, labels = c("o1","o2","o3") ) 
  )  ,
  b = data.frame(
    x = c(4,5), 
    y = factor(c(3,3), levels = 1:3, labels = c("f1","f2","f3") ),
    z = ordered(c(3,2), levels = 1:3, labels = c("o1", "o2","o3") ) 
  )
)

> input
$a:
  x  y  z 
1 1 f1 o2
2 2 f1 o2
3 3 f2 o1

$b:
  x  y  z 
1 4 f3 o3
2 5 f3 o2

output <- data.frame(
    x = c(1.0,2,3,4,5) , 
    y = factor(c(1,1,2,3,3), levels = 1:3, labels = c("f1", "f2", 
"f3") ) ,
    z = ordered(c(2,2,1,3,2), levels = 1:3, labels = 
c("o1","o2","o3") ) 
  ) 

> output
  x  y  z 
1 1 f1 o2
2 2 f1 o2
3 3 f2 o1
4 4 f3 o3
5 5 f3 o2

john.gavin@ubs.com

John Gavin
Quantitative Risk Models and Statistics
Warburg Dillon Read
1 Finsbury Ave (4th floor)
London EC2M 2PP, UK


This message contains confidential information and is intended only 
for the individual named.  If you are not the named addressee you 
should not disseminate, distribute or copy this e-mail.  Please 
notify the sender immediately by e-mail if you have received this 
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free 
as information could be intercepted, corrupted, lost, destroyed, 
arrive late or incomplete, or contain viruses.  The sender therefore 
does not accept liability for any errors or omissions in the contents 
of this message which arise as a result of e-mail transmission.  If 
verification is required please request a hard-copy version.  This 
message is provided for informational purposes and should not be 
construed as a solicitation or offer to buy or sell any securities or 
related financial instruments.

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post