[R] how to merge these dataframes

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 22 09:52:20 CET 2006


merge() merges *two* data frames, as is very clearly stated on its help 
page.  In your first version, j3 is matching by.x (and the second is 
wrong, as j does not exist and names() applies to an object).

You can do

j4 <- merge(j1, j2, by="Var1", all=TRUE)
j5 <- merge(j4, j3, all=TRUE)
j5
          Var1 Freq.x Freq.y Freq
1  1988-01-13      1      1    1
2  1988-01-16      1      1    1
3  1988-01-20      3      3    3
4  1988-01-25      2      2    2
5  1988-01-30      1      1    1
6  1988-02-01      5      4    4
7  1988-02-08      4      4    4
8  1988-02-14      1     NA   NA
9  1988-02-16      1      1    1
10 1988-02-18      4      4    4
11 1988-02-24     NA      2   NA
12 1988-03-04     NA      1   NA
13 1988-03-07     NA      1   NA

(or omit all=TRUE if you only want columns which are in all 3 data 
frames).


On Wed, 22 Nov 2006, antonio rodriguez wrote:

> Hi,
>
> Having 3 dataframes with different row numbers, but equal column names
> (see below) I want to merge them by Var1 so I've tried:
>
> merge(j1,j2,j3,by="Var1")
> merge(j,j1,j2,by=names("Var1"))
>
> But always got the same message:
>
> Erro en fix.by(by.x, x) : 'by' must specify column(s) as numbers, names
> or logical
>
> What I'm doing wrong?
>
> Thanks,
>
> Antonio
>
>
> j1
>
>            Var1 Freq
> 1  1988-01-13    1
> 2  1988-01-16    1
> 3  1988-01-20    3
> 4  1988-01-25    2
> 5  1988-01-30    1
> 6  1988-02-01    5
> 7  1988-02-08    4
> 8  1988-02-14    1
> 9  1988-02-16    1
> 10 1988-02-18    4
>
> j2
>
>            Var1 Freq
> 1  1988-01-13    1
> 2  1988-01-16    1
> 3  1988-01-20    3
> 4  1988-01-25    2
> 5  1988-01-30    1
> 6  1988-02-01    4
> 7  1988-02-08    4
> 8  1988-02-16    1
> 9  1988-02-18    4
> 10 1988-02-24    2
> 11 1988-03-04    1
> 12 1988-03-07    1
>
> j3
>          Var1 Freq
> 1 1988-01-13    1
> 2 1988-01-16    1
> 3 1988-01-20    3
> 4 1988-01-25    2
> 5 1988-01-30    1
> 6 1988-02-01    4
> 7 1988-02-08    4
> 8 1988-02-16    1
> 9 1988-02-18    4
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list