[R] Joining two files

jim holtman jholtman at gmail.com
Fri Nov 23 16:20:06 CET 2012


You did not specify what you were expecting as output.  Here is one
way of using 'merge', but I am not sure if this is what you were
after:

> r1<-c(1,1,2,3)
> r2<-c(2,1,2,2)
> r3<-c(2,1,4,1)
> data1<-data.frame(r1,r2)
> data2<-data.frame(r1,r3)
> data1
  r1 r2
1  1  2
2  1  1
3  2  2
4  3  2
> data2
  r1 r3
1  1  2
2  1  1
3  2  4
4  3  1
> merge(data1, data2, by = "r1", all = TRUE)
  r1 r2 r3
1  1  2  2
2  1  2  1
3  1  1  2
4  1  1  1
5  2  2  4
6  3  2  1
>


On Fri, Nov 23, 2012 at 10:11 AM, Virgile Capo-Chichi
<vcapochichi at gmail.com> wrote:
> Hi Jim,
> I did not try merge because I thought it only adds variables instead of
> cases. Below is what I am trying to do. When I joined data1 and data2, I
> was was expecting three variables: r1, r2 and r3 with r2 and r3 presenting
> missing values where they did not exist in the first place. V
>
>> r1<-c(1,1,2,3)
>> r2<-c(2,1,2,2)
>> r3<-c(2,1,4,1)
>> data1<-cbind(r1,r2)
>> data2<-cbind(r1,r3)
>> data1
>      r1 r2
> [1,]  1  2
> [2,]  1  1
> [3,]  2  2
> [4,]  3  2
>> data2
>      r1 r3
> [1,]  1  2
> [2,]  1  1
> [3,]  2  4
> [4,]  3  1
>> data<-rbind(data1, data2)
>> data
>      r1 r2
> [1,]  1  2
> [2,]  1  1
> [3,]  2  2
> [4,]  3  2
> [5,]  1  2
> [6,]  1  1
> [7,]  2  4
> [8,]  3  1
>> data3<-cbind(r2, r1)
>> data_test<-rbind(data1, data3)
>> data1
>      r1 r2
> [1,]  1  2
> [2,]  1  1
> [3,]  2  2
> [4,]  3  2
>> data3
>      r2 r1
> [1,]  2  1
> [2,]  1  1
> [3,]  2  2
> [4,]  2  3
>> data_test
>      r1 r2
> [1,]  1  2
> [2,]  1  1
> [3,]  2  2
> [4,]  3  2
> [5,]  2  1
> [6,]  1  1
> [7,]  2  2
> [8,]  2  3
>
>
> 2012/11/23 jim holtman <jholtman at gmail.com>
>
>> Have you tried 'merge'?
>>
>> You did not provide any sample data (use 'dput' if you do) so that we
>> could show a possible solution.
>>
>> On Fri, Nov 23, 2012 at 9:56 AM, Virgile Capo-Chichi
>> <vcapochichi at gmail.com> wrote:
>> > Hello all,
>> > I al trying to join (ADD FILES in SPSS) two files using the rbind()
>> > function. However, with rbind() R does not behave the same way as SPSS. I
>> > mean, it just concatenates the two blocs with no consideration for same
>> > variables if these are not in the same position in the two files. Anyone
>> > knows a function that performs the SPSS ADD FILES task? Thanks, V
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org 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.
>>
>>
>>
>> --
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list