Skip to content

Merging data.frames

4 messages · Mark Lamias, Rick Bilonick, Kaspar Pflugshaupt +1 more

#
On Tuesday, March 07, 2000 5:40 PM, Richard Bilonick wrote:

            
The easiest way to to this, I think, is as follows:

if you have several data frames, dataframe1, dataframe2, . . . , dataframen,
you can merger them all into one dataframe by using the data.frame command
again with the individuaal data.frames as arguments:

	data.frame(dataframe1, dataframe2, . . . , dataframen)

here's a quick example:
a1 a2
1  4  6
2  4  6
3  4  6
a1 a2 b1 b2
1  1  2  3  4
2  1  2  3  4
3  1  2  3  4
a1 a2 b1 b2 a1 a2
1  1  2  3  4  1  2
2  1  2  3  4  1  2
3  1  2  3  4  1  2

I hope this helps.

Mark J. Lamias
Department of Statistics
Department of Political Science
Survey Methodology Program/Survey Research Center
Institute for Social Research - University of Michigan
426 Thompson Street, Room 315
Ann Arbor, Michigan 48104-2321
(734) 647-5381
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Mark Lamias wrote:

            
Unfortunately it does not help. I need to concatenate the records vertically, not
horizontally. Each data.frame has the same variables. One data.frame may have 10
records and the other has 20 records. I want a new data.frame with all 30 records.
If these were matrices I would use rbind. Unfortunately rbind does not work nicely
with data.frames.

Rick Bilonick

--
Statistical Consulting for Business & Industry
mailto:rab at nauticom.net
efax:  508 445 5821
voice: 412 831 4509



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hello Richard,

I had the a similar problem two weeks ago. In my case, I wanted to re-merge
a dataframe that I had splitted up into a list using split(), then added
variables to each component (the same varnames everywhere). rbind seems to
work only for two dataframes at a time. So, in the end I used

unlist<-function (splitlist)
{
tempframe<-splitlist[[1]]
for (i in 2:length(splitlist))
tempframe<-rbind(tempframe,splitlist[[i]])
tempframe
}

which is not elegant in any way (R-cracks look away), but works. There may
be a very efficient and terse solution for this...

Cheers

Kaspar

  
    
#
On Wed, 8 Mar 2000, Richard A. Bilonick wrote:
What goes wrong with rbind?  It's supposed to work on data.frames
and there's a method rbind.data.frame()

R> d<-data.frame(a=1:10,b=2:11,c=3:12)
R> e<-data.frame(a=101:110,b=102:11,c=103:112)
R> rbind(d,e)
     a   b   c
1    1   2   3
2    2   3   4
3    3   4   5
4    4   5   6
5    5   6   7
6    6   7   8
7    7   8   9
8    8   9  10
9    9  10  11
10  10  11  12
11 101 102 103
12 102 103 104
13 103 104 105
14 104 105 106
15 105 106 107
16 106 107 108
17 107 108 109
18 108 109 110
19 109 110 111
20 110 111 112


	-thomas

Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._