s-news
[Top] [All Lists]

Summary: rbind with a NULL data.frame

To: "S-News (E-mail)" <s-news@wubios.wustl.edu>
Subject: Summary: rbind with a NULL data.frame
From: "Wright, Kevin" <WRIGHTKEVI@phibred.com>
Date: Thu, 15 Feb 2001 15:59:21 -0600
Original post:
--------------
I'm interested in using rbind inside a loop and would like to somehow
intialize a data.frame to which I can rbind a row every iteration.  This
works with matrices, for example:

A_NULL
rbind(A, 1:5)

but I cannot make it work with data.frame.  


Solutions:
----------
1. Several people suggested creating a dummy row for the data.frame and then
deleting the row after the completion of the loop.

2. Bill Dunlap at Insightful came up with an almost perfect solution:

> A <- data.frame(One=numeric(0),
Two=factor(character(0),levels=c("No","Yes")))
> rbind(A, data.frame(One=pi, Two="Yes"))
       One Two
1 3.141593 Yes

If a person needs to avoid factors then you have to be clever, since 
> data.frame(One=numeric(0), Two=character(0))
does NOT allow rbind to work since S-Plus thinks Two is a factor.  
However, I came up with this twist:

  A <- data.frame(One=numeric(0), Two=character(0))
  A$Two_as.character(A$Two)
  rbind(A, data.frame(One=pi, Two="Yes"))

and Leonid Gibiansky discovered this (unintuitive) trick:

  A <- data.frame(a=numeric(0),b=double(0))
  rbind(A, data.frame(a=5,b="blue") )


Thanks to everyone,

Kevin

Kevin Wright, Research Scientist
Pioneer Hi-Bred Int'l, x4054.



<Prev in Thread] Current Thread [Next in Thread>
  • Summary: rbind with a NULL data.frame, Wright, Kevin <=