【发布时间】: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