Reconcile and update column in data table R

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
+4
source share
3 answers

You tried:

dt1[match(email,dt2$emails), custtype:="some value"]

match(), dt2, dt1. .

, dt2 dt1.

> dt1
             email custtype
1:   abc@yahoo.com     subs
2:   eli@gmail.com     subs
3: tod@hotmail.com     subs

> dt1[dt2, on=c(email="emails"), custtype:="some value"]

> dt1
             email   custtype
1:   abc@yahoo.com       subs
2:   eli@gmail.com       subs
3: tod@hotmail.com some value

, , , , .

FAQ 2.14 , A[B].

+3

data.table :

dt2[dt1, on = c(emails = "email"), mult = "first"][!is.na(range), custtype := "some value"][, range := NULL][]

dt2[dt1, on = c(emails = "email"), mult = "first"]

- , dt1:

            emails  range custtype
1:   abc@yahoo.com     NA     subs
2:   eli@gmail.com     NA     subs
3: tod@hotmail.com orange     subs

mult = "first" dt2 , range.

custtype

-NA range.

[!is.na(range), custtype := "some value"]

, custtype , .

range

range .

[, range := NULL]

final

[]

, .

+1

Found a solution using. But still looking for a more elegant solution.

dt1[which(match(email, dt2$emails) > 0), custtype := "some value"]
0
source

Source: https://habr.com/ru/post/1657317/


All Articles