I want to join "Division" from table2 to "Industry" in table1. To do so I will have to match the HSICCD from tablet1 that is between "from" and "to" in table2.
Just to be clear: If HSICCD in table1 is between from/to in table2, I want to take value from table2$division and add it to table1$industry(Or a new column like in a join).
Is there a join function in R that does this without too much hassle? (dplyr solutions are desired, but I'm glad for every contribution!)
**Table1:**
PERMNO HSICCD Industry
<dbl> <dbl> <lgl>
1 10000 3990 NA
2 10001 4925 NA
3 10002 6020 NA
4 10003 6020 NA
5 10004 5330 NA
6 10005 1310 NA
7 10006 3743 NA
8 10007 7370 NA
9 10008 3430 NA
10 10009 6030 NA
**Table2:**
from to division
<dbl> <dbl> <chr>
1 100 999 Agriculture
2 1000 1499 Mining
3 1500 1799 Construction
4 1800 1999 Other
5 2000 3999 Manufacturing
6 4000 4999 Transportation
7 5000 5199 Wholesale
8 5200 5999 Retail
9 6000 6799 Finance
10 7000 8999 Services
11 9100 9729 Public
12 9900 9999 Other
My only solution so far is this horrendous code:
Compustat_identifiers$Industry <- NA
for (hsiccd in 1:nrow(Compustat_identifiers)) {
for (SIC in 1:nrow(sic_table)) {
if (is.na(Compustat_identifiers$HSICCD[hsiccd]) == T) {
Compustat_identifiers$Industry[hsiccd] <- "Other"
} else if (Compustat_identifiers$HSICCD[hsiccd] >= sic_table$from[SIC] &
Compustat_identifiers$HSICCD[hsiccd] <= sic_table$to[SIC]) {
Compustat_identifiers$Industry[hsiccd] <- sic_table$division[SIC]
}
}
}