Hello,
So you need a function that can rbind and also return the total unique
columns. It's a bit more complicated but I think this does what you want.
ADD <- function(x, y){
cx <- colnames(x)
cy <- colnames(y)
if(is.null(cx) || is.null(cy)){
d <- length(cx) - length(cy)
if(d == 0){
result <- rbind(x, y)
}else{
result <- matrix(NA, nrow = nrow(x), ncol = abs(d))
if(d < 0){
result <- cbind(x, result)
result <- rbind(result, y)
}else{
result <- cbind(y, result)
result <- rbind(x, result)
}
}
}else{
nx <- nrow(x)
ny <- nrow(y)
result <- matrix(nrow = nx + ny, ncol = length(unique(c(cx, cy))))
colnames(result) <- unique(c(cx, cy))
result[seq_len(nx), cx] <- x
result[nx + seq_len(ny), cy] <- y
}
result
}
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)
data3<-cbind(r2, r1)
ADD(data1, data2)
ADD(data1, data3)
Hope this helps,
Rui Barradas
Em 23-11-2012 15:57, Virgile Capo-Chichi escreveu:
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 <jholt...@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
<vcapochi...@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 <jholt...@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
<vcapochi...@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@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@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@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@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.