[R] Joining two files
David L Carlson
dcarlson at tamu.edu
Fri Nov 23 18:21:38 CET 2012
Arun's solution can be modified slightly to be more general:
r1<-c(1,1,2,3)
r2<-c(2,1,2,2)
r3<-c(2,1,4,1)
r4<-c(3,4,1,2)
r5<-c(3,1,2,2)
data1<-data.frame(r1,r2,r4)
data2<-data.frame(r1,r3,r5)
data1[,setdiff(names(data2), names(data1))] <- NA
data2[,setdiff(names(data1), names(data2))] <- NA
data3<-rbind(data1[,order(names(data1))], data2[,order(names(data2))])
data3
# r1 r2 r3 r4 r5
# 1 1 2 NA 3 NA
# 2 1 1 NA 4 NA
# 3 2 2 NA 1 NA
# 4 3 2 NA 2 NA
# 5 1 NA 2 NA 3
# 6 1 NA 1 NA 1
# 7 2 NA 4 NA 2
# 8 3 NA 1 NA 2
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Friday, November 23, 2012 10:56 AM
> To: Virgile Capo-Chichi
> Cc: R help
> Subject: Re: [R] Joining two files
>
> Hi,
> Try this:
>
> 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$r3<-NA
> data2$r2<-NA
> merge(data1,data2,all=TRUE,sort=FALSE)
> # r1 r2 r3
> #1 1 2 NA
> #2 1 1 NA
> #3 2 2 NA
> #4 3 2 NA
> #5 1 NA 2
> #6 1 NA 1
> #7 2 NA 4
> #8 3 NA 1
> A.K.
>
>
>
> ----- Original Message -----
> From: Virgile Capo-Chichi <vcapochichi at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Friday, November 23, 2012 10:57 AM
> Subject: Re: [R] Joining two files
>
> Thanks Jim for your help. Your results are not what I wanted. I would
> like
> to see something like the matrix below. This is what I would get if I
> used
> the ADD Files command in SPSS. V
>
> r1 r2 r3
> 1 2 NA
> 1 1 NA
> 2 2 NA
> 3 2 NA
> 1 NA 2
> 1 NA 1
> 2 NA 4
> 3 NA 1
>
>
> 2012/11/23 jim holtman <jholtman at gmail.com>
>
> > 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.
> >
>
> [[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.
>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list