[R] concatenate 2 data.frames
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Mar 23 10:41:34 CET 2007
On Fri, 2007-03-23 at 08:51 +0100, João Fadista wrote:
> Dear all,
>
> I would like to know how can I concatenate 2 data.frames into a single
> one. Both data frames have the same number of columns and the same
> class type in each correspondent column. So what I want is to have a
> new data.frame where I have first the values from one data.frame and
> then the values from a second data.frame would came after in this new
> data.frame.
>
> Thanks in advance.
By "after", do you mean columns for dataframe1 then columns of
dataframe2, or do you mean you want to append dataframe2 onto the bottom
of dataframe1?
The first is:
dat1 <- data.frame(var1 = rnorm(10), var2 = rnorm(10),
var3 = gl(2, 5, labels = c("red", "blue")))
dat2 <- data.frame(var4 = rnorm(10), var5 = rnorm(10),
var6 = gl(2, 5, labels = c("red", "blue")))
combined <- data.frame(dat1, dat2)
combined
var1 var2 var3 var4 var5 var6
1 -1.61397560 -0.40296928 red 1.48380888 1.35501273 red
2 1.01901681 -0.27616320 red -1.00234243 -0.79328309 red
3 -0.88272375 -0.42375566 red -1.31503261 -0.04570735 red
4 1.37368014 -0.63154987 red -1.40635604 1.50906371 red
5 0.66810230 -0.43453383 red 0.30449564 -0.24893343 red
6 -0.06403118 -1.59095216 blue 0.41945472 0.09143192 blue
7 0.02208197 1.70299530 blue -1.64188953 -0.30545702 blue
8 -1.13057000 -0.67610437 blue -1.15801044 1.17682587 blue
9 -2.32315433 -0.07500192 blue 0.03576081 -1.14670543 blue
10 -0.64734307 0.74789423 blue -0.57466841 -1.69753353 blue
You could also use cbind().
The second could be:
## need to provide the same variables names for matching columns
names(dat2) <- c("var1", "var2", "var3")
rbind(dat1, dat2)
var1 var2 var3
1 -1.61397560 -0.40296928 red
2 1.01901681 -0.27616320 red
3 -0.88272375 -0.42375566 red
4 1.37368014 -0.63154987 red
5 0.66810230 -0.43453383 red
6 -0.06403118 -1.59095216 blue
7 0.02208197 1.70299530 blue
8 -1.13057000 -0.67610437 blue
9 -2.32315433 -0.07500192 blue
10 -0.64734307 0.74789423 blue
11 1.48380888 1.35501273 red
12 -1.00234243 -0.79328309 red
13 -1.31503261 -0.04570735 red
14 -1.40635604 1.50906371 red
15 0.30449564 -0.24893343 red
16 0.41945472 0.09143192 blue
17 -1.64188953 -0.30545702 blue
18 -1.15801044 1.17682587 blue
19 0.03576081 -1.14670543 blue
20 -0.57466841 -1.69753353 blue
HTH
G
>
>
> Med venlig hilsen / Regards
>
> Joo Fadista
> Ph.d. studerende / Ph.d. student
>
>
>
> AARHUS UNIVERSITET / UNIVERSITY OF AARHUS
> Det Jordbrugsvidenskabelige Fakultet / Faculty of Agricultural Sciences
> Forskningscenter Foulum / Research Centre Foulum
> Genetik og Bioteknologi / Dept. of Genetics and Biotechnology
> Blichers All 20, P.O. BOX 50
> DK-8830 Tjele
>
> Tel: +45 8999 1900
> Direct: +45 8999 1900
> Mobile: +45
> E-mail: Joao.Fadista at agrsci.dk <mailto:Joao.Fadista at agrsci.dk>
> Web: www.agrsci.dk <http://www.agrsci.dk/>
> ________________________________
>
> Tilmeld dig DJF's nyhedsbrev / Subscribe Faculty of Agricultural Sciences Newsletter <http://www.agrsci.dk/user/register?lan=dan-DK> .
>
> Denne email kan indeholde fortrolig information. Enhver brug eller offentliggrelse af denne email uden skriftlig tilladelse fra DJF er ikke tilladt. Hvis De ikke er den tiltnkte adressat, bedes De venligst straks underrette DJF samt slette emailen.
>
> This email may contain information that is confidential. Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed. If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email.
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [t] +44 (0)20 7679 0522
ECRC [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list