【问题标题】:How best to do this join in R?如何最好地加入 R?
【发布时间】:2022-09-27 16:51:57
【问题描述】:

下面是样本数据。我知道我必须做一个左连接。问题是如何让它只返回匹配 (indcodelist = indcodelist2) 但具有最高代码类型值的值。

 indcodelist <- c(110000,111000,112000,113000,114000,115000,121000,210000,211000,315000)
 estemp <- c(11,21,31,41,51,61,55,21,22,874)
 projemp <- c(15,25,36,45,52,61,31,29,31,899)
 nchg <- c(4,4,5,4,1,0,-24,8,9,25)


 firsttable <- data.frame(indcodelist,estemp,projemp,nchg)


  indcodelist2 <- c(110000,111000,112000,113000,114000,115000,121000,210000,211000,315000,110000,111000,112000,113000)
  codetype <- c(18,18,18,18,18,18,18,18,18,18,10,10,10,10)
  codetitle <- c(\"Accountant\",\"Doctor\",\"Lawyer\",\"Teacher\",\"Economist\",\"Financial Analyst\",\"Meteorologist\",\"Dentist\", \"Editor\",\"Veterinarian\",\"Accounting Technician\",\"Doctor\",\"Lawyer\",\"Teacher\")
  secondtable <- data.frame(indcodelist2,codetype,codetitle)


  tried <- left_join(firsttable,secondtable, by =c(indcodelist = \"indcodelist2\"))

  Desired Result

  indcodelist       estemp     projemp      nchg        codetitle   

  110000              11          15           4           Accountant
  111000              21          25           4           Doctor

    标签: r dplyr left-join


    【解决方案1】:

    如果您只想要两个表中匹配的值,inner_join 可能就是您要查找的内容。您可以查看this answer 以了解不同类型的联接。

    要获得最高的codetype,可以使用dplyr::slice_max()。请注意,默认行为是返回相关的值。如果在同一个codetype 中有多个codetitle,它们都将被退回。

    library(tidyverse)
    
    firsttable %>%
      inner_join(., secondtable, by = c("indcodelist" = "indcodelist2")) %>%
      group_by(indcodelist) %>%
      slice_max(codetype)
    #> # A tibble: 10 × 6
    #> # Groups:   indcodelist [10]
    #>    indcodelist estemp projemp  nchg codetype codetitle        
    #>          <dbl>  <dbl>   <dbl> <dbl>    <dbl> <chr>            
    #>  1      110000     11      15     4       18 Accountant       
    #>  2      111000     21      25     4       18 Doctor           
    #>  3      112000     31      36     5       18 Lawyer           
    #>  4      113000     41      45     4       18 Teacher          
    #>  5      114000     51      52     1       18 Economist        
    #>  6      115000     61      61     0       18 Financial Analyst
    #>  7      121000     55      31   -24       18 Meteorologist    
    #>  8      210000     21      29     8       18 Dentist          
    #>  9      211000     22      31     9       18 Editor           
    #> 10      315000    874     899    25       18 Veterinarian
    

    reprex package (v2.0.1) 于 2022 年 9 月 15 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2013-07-02
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多