[R] Joining two files

Rui Barradas ruipbarradas at sapo.pt
Fri Nov 23 16:50:12 CET 2012


Hello,

I don't believe there's an R function that does what you want, but you 
can write one.

myrbind <- function(x, y){
     cx <- colnames(x)
     cy <- colnames(y)
     if(is.null(cx) || is.null(cy)){
         result <- rbind(x, y)
     }else{
         ox <- order(cx)
         oy <- order(cy)
         result <- rbind(x[, ox], y[, oy])
         result <- result[, order(ox)]
     }
     result
}

myrbind(data1, data3)
      r1 r2
[1,]  1  2
[2,]  1  1
[3,]  2  2
[4,]  3  2
[5,]  1  2
[6,]  1  1
[7,]  2  2
[8,]  3  2


Hope this helps,

Rui Barradas
Em 23-11-2012 15:11, Virgile Capo-Chichi escreveu:
> 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.




More information about the R-help mailing list