Is there a way to create Stata's _merge indicator variable with R's merge()?

前端 未结 3 1522
再見小時候
再見小時候 2021-01-18 21:18

Stata automatically creates a variable called \"_merge\" indicating the matched variables in both datasets after merge. Is there a way to get such variable generated by R\'s

3条回答
  •  情书的邮戳
    2021-01-18 21:28

    Here is (I think) a far simpler and more efficient version of the previous person's stata.merge function. This assumes you don't have variables named "new1" or "new2" in your data frames. If this assumption is wrong, change the variable names in this function. This function takes 3 variables, the first data frame, the second data frame, and the value to enter into the "by =" part of the merge function.

    stata.merge <- function(x,y, name){
      x$new1 <- 1
      y$new2 <- 2
      df <- merge(x,y, by = name, all = TRUE)
      df$stat.merge.variable <- rowSums(df[,c("new1", "new2")], na.rm=TRUE)
      df$new1 <- NULL
      df$new2<- NULL
      df
    }
    

提交回复
热议问题