另外,略有不同,方法是左外連接兩者A
和B
到C
第一,然後找到該連接將添加的列等於C1
:
## Do the left outer joins with merge by n and all.x=TRUE
out <- merge(merge(C,A,by="n",all.x=TRUE),B,by="n",all.x=TRUE)
## Loop over rows and extract the name of the column whose value matches C1
## first define a function to do so
extract.name <- function(i,out) {
j <- which(out$C1[i]==out[i,3:ncol(out)])
if (length(j)==0) return("0") else return(substr(colnames(out)[j[1]+2],1,1))
}
## Then, apply it to all rows
out$C1 <- sapply(1:nrow(out),extract.name,out)
## Keep only the n and C1 columns as output
out <- out[,1:2]
## n C1
##1 1 B
##2 1 A
##3 1 B
##4 2 0
##5 2 A
##6 2 A
##7 3 B
##8 3 0
##9 3 B
數據:
A <- structure(list(A1 = c(1L, 3L, 2L), A2 = c(2L, 2L, 4L), n = 1:3), .Names = c("A1",
"A2", "n"), class = "data.frame", row.names = c("1", "2", "3"
))
## A1 A2 n
##1 1 2 1
##2 3 2 2
##3 2 4 3
B <- structure(list(B1 = c(3L, 4L, 1L), B2 = c(4L, 1L, 3L), n = 1:3), .Names = c("B1",
"B2", "n"), class = "data.frame", row.names = c("1", "2", "3"
))
## B1 B2 n
##1 3 4 1
##2 4 1 2
##3 1 3 3
C <- structure(list(n = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), C1 = c(3L,
1L, 4L, 0L, 2L, 3L, 3L, 0L, 1L)), .Names = c("n", "C1"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9"))
## n C1
##1 1 3
##2 1 1
##3 1 4
##4 2 0
##5 2 2
##6 2 3
##7 3 3
##8 3 0
##9 3 1