Merge dataframes in R with different size and condition

前端 未结 2 1188
南旧
南旧 2021-01-21 15:43

I\'m trying to combine 2 csv files into one file. They have common id with different size. I used merge() but I got replicated data. I have the following data-frames;

         


        
2条回答
  •  温柔的废话
    2021-01-21 16:31

    An option is regex_left_join from fuzzyjoin

    library(fuzzyjoin)
    library(dplyr)
    regex_left_join(data2, data1, by = c("SR", "class" = "school")) %>%
          select(SR = SR.x, school, class, Y)
    
    #    SR    school   class    Y
    # 1  SR1    S-1     S-1.2    3
    # 2  SR1    S-1     S-1.5    3
    # 3  SR1    S-1     S-1.7    3
    # 4  SR2    S-1     S-1.1    4
    # 5  SR2    S-1     S-1.2    4
    # 6  SR2    S-1     S-1.3    4
    # 7  SR2    S-1     S-1.6    4
    # 8  SR2    S-2     S-2.3    1
    # 9  SR2    S-2     S-2.9    1
    # 10 SR2    S-4     S-4.2    2
    # 11 SR3    S-2     S-2.1    5
    # 12 SR3    S-2     S-2.3    5
    # 13 SR4    S-1     S-1.5    2
    # 14 SR4    S-1     S-1.6    2
    # 15 SR4    S-5     S-5.1    3
    

提交回复
热议问题