I am trying to conditionally replace row values in a data table.Please consider the following dataset excerpt:
library(data.table)txt1 <- "Date Location Measurement Scenario Var Month Decade 1960-01-01 4100103 23.3 CRU3.2 Temperature Jan 1960-1990 1960-02-01 4100103 24.1 CRU3.2 Temperature Feb 1960-1990 1960-03-01 4100103 23.6 CRU3.2 Temperature Mar 1960-1990 1960-04-01 4100103 20.4 CRU3.2 Temperature Apr 1960-1990 1960-05-01 4100103 16.2 CRU3.2 Temperature May 1960-1990 1960-06-01 4100103 16.5 CRU3.2 Temperature Jun 1960-1990"dt <- data.table(read.table(textConnection(txt1), header=TRUE))
This is just a sample. My actual data has about 2.5 million rows.
As you can see, I have temperature measurements for multiple locations.However, the locations are identified with geocode instead of names, which is not very readable.
Therefore, I have another dataset that relates geocode and city name:
txt2 <- "GEOCODIG_M, Name4100103, Abatiá4100202, Adrianópolis4100301, Agudos do Sul4100400, Almirante Tamandaré4100459, Altamira do Paraná4100509, Altônia"df <- read.csv(textConnection(txt2),sep=',', header=TRUE)
So, what I need to do is compare the field Location
in dt
to GEOCODIG_M
in df
and replace geocode by name.
The expected outcome for this case would be:
Date Name Measurement Scenario Var Month Decade 1960-01-01 Abatiá 23.3 CRU3.2 Temperature Jan 1960-1990 1960-02-01 Abatiá 24.1 CRU3.2 Temperature Feb 1960-1990 1960-03-01 Abatiá 23.6 CRU3.2 Temperature Mar 1960-1990 1960-04-01 Abatiá 20.4 CRU3.2 Temperature Apr 1960-1990 1960-05-01 Abatiá 16.2 CRU3.2 Temperature May 1960-1990 1960-06-01 Abatiá 16.5 CRU3.2 Temperature Jun 1960-1990
What is the best way to do this using data tables?