在联接之前,如何识别两个数据帧之间不匹配的ID?

时间:2019-02-03 17:58:40

标签: r dataframe join missing-data

我正在创建一个函数,以在将两个数据框连接在一起之前识别出它们之间的ID。

到目前为止,我的功能如下:

match_check <- function(df1,var1,df2,var2){
  df1ids <- unique(df1$var1)
  matchs <- c()
  no_matchs <- c() 
  for (id in df1ids){
    if (id %in% df2$var2 == TRUE){
      match <- append(match, id)}
     else{
       no_matchs <- append(no_match,id)
     }
  }
  print(no_matchs)
  match2 <- c()
   no_match2 <- c()
  df2ids <- unique(df2$var2)
  for (id in df2ids){
    if (id %in% df1$var1 == TRUE){
      match2 <- append(match2, id)}
     else{
      no_match2 <- append(no_match2,id)
    }
  }
  print(no_match2)
 }

test1 <- data.frame(id=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15))
test2 <- data.frame(id=c(0,-2,-4,-6,-1,1,2,3,4,5,5,6,7,8))

match_check(test1,id,test2,id)

当我运行函数时,打印的矢量将打印为NULL。 我希望它打印出彼此之间找不到的ID,因此我知道彼此之间缺少的ID,并给出如下所示的向量:

no_matchs = c(9,10,11,12,13,14,15)
no_match2 = c(0,-2,-4,-6)

1 个答案:

答案 0 :(得分:2)

df_grouped = df.groupby(['program']).sum()
相关问题