#' Merge two data frames and return the result with the contribution indices
#'
#' @param x First data frame.
#' @param y Second data frame.
#' @param ... parameters to the native merge frunction.
#' @param .inx. The name of the contribution index for x.
#' @param .iny. The name of the contribution index for y.
#'
#' @details
#' The SAS like contribution indices will be returned as the variables
#' of the merged data frame.
#'
#' In the followint example, those observations with \code{in.x=1} are
#' contributed from data frame x, and those with \code{in.y=1}, from y.
#'
#' @examples
#' d1 <- data.frame(x=c(11:15, 17:20),y=c(111:115, 117:120))
#' d1[2,2] <- NA
#' d2 <- data.frame(x=c(11:13, 16, 18:21),y=c(211:213, 216, 218:221))
#' d2[5,2] <- NA
#' d12 <- merge2(d1,d2, by="x", all=TRUE)
#' printm( d1, d2, d12 )
#'
#' D12 <- merge2(d1,d2, by="x", all=TRUE, .inx.="IN1")
#' printm( d1, d2, D12 )
#'
#' @export
#'


merge2 <- function( x, y, ..., .inx.="in.x", .iny.="in.y" ){
 # merge two data frames and return the result with the contribution indices
 # Shin-ichi Mayekawa
 # 20220908
 #

 D12=merge( data.frame(x, ..id..x=1:nrow(x) )
          , data.frame(y, ..id..y=1:nrow(y) )
          , ... )
 in1=as.integer(!is.na(D12$..id..x))
 in2=as.integer(!is.na(D12$..id..y))

 D12=D12[,!(colnames(D12) %in% c("..id..x","..id..y"))]
 cname=colnames(D12)
 D12=data.frame(D12,in1,in2)
 colnames(D12)=c(cname, .inx., .iny.)

 return( D12 )

} # end of merge2


