Replace DT1.x with DT2.y when DT1.x and DT2.x are the same in R
I want to do this using data tables in R.
So I start with this
dtMain
Name state
1: CompanyC CA
2: CompanyM MN
3: CompanyC1 California
4: CompanyT TX
statesFile
stateExpan state
1: Texas TX
2: Minnesota MN
3: California CA
Where dtMain$State == statesFile$state
, I want to replace dtMain$State
with statesFile$stateExpan
and get this
dtMain
Name state
1: CompanyA California
2: CompanyB Minnesota
3: CompanyC California
4: CompanyD Texas
Here's the code to create 2 files
library(data.table)
dtMain <- data.table(Name = c("CompanyA" ,"CompanyB","CompanyC","CompanyD"),
state = c("CA","MN","California","TX"))
statesFile <- data.table( stateExpan = c("Texas","Minnesota","California"),
state = c("TX","MN","CA"))
My problem is the next level of this R is searching for dataframe rows where some columns match other columns and I'm looking for a data table solution.
+1
source to share