I have 2 data tables. The dput and data tables are shown below:
Dt1
email custtype
1: abc@yahoo.com subs
2: eli@gmail.com subs
3: tod@hotmail.com subs
dt1 = setDT(structure(list(email = c("abc@yahoo.com", "eli@gmail.com", "tod@hotmail.com"
), custtype = c("subs", "subs", "subs")), .Names = c("email",
"custtype"), class = c("data.table", "data.frame"), row.names = c(NA,
-3L)))
dt2
emails range
1: sam@live.com orange
2: tod@hotmail.com orange
3: ver@live.com orange
4: yahoo@yah.com orange
dt2 = setDT(structure(list(emails = structure(1:4, .Label = c("sam@live.com",
"tod@hotmail.com", "ver@live.com", "yahoo@yah.com"), class = "factor"),
range = structure(c(1L, 1L, 1L, 1L), .Label = "orange", class = "factor")), .Names = c("emails",
"range"), class = c("data.table", "data.frame"), row.names = c(NA,
-4L)))
I am trying to map an email from dt1 to emails in dt2, and then trying to update the custtype column in dt1. So far i tried
dt1[match(email,dt2$emails), custtype:="some value"]
This means that it changes the value in the index where the match was found, rather than changing it to the email address for which the match was found.
The resulting data table should look like this. The letter for which a match was found, the corresponding custtype must be supplemented with the value provided:
email custtype
1: abc@yahoo.com subs
2: eli@gmail.com subs
3: tod@hotmail.com some value