Displaying .table data based on another data.table

I have two data

( .xlsx

), DT1

and DT2

. I want to create a new column newcol

in DT1

based on the original column in DT1

, mapping to columns in DT2

.
I know this is ambiguous, so I'll explain more here:
First, here are my two data.

DT1
code    type  
AH1     AM
AS5     AM
NMR     AM
TOS     AM
IP      AD
CC      ADCE
CA      Wa

DT2
code    year   month  
AH1     2011   2
AH1     2011   5
AS5     2012   7
AS5     2012   6
AS5     2013   3
CC      2014   6
CA      2016   11

      

Second, the DT2

columns year

and are month

irrelevant in this matter. We don't need to explain this.
Third, I want:

DT2
code    year   month   newcol
AH1     2011   2       AM
AH1     2011   5       AM
AS5     2012   7       AM
AS5     2012   6       AM
AS5     2013   3       AM
CC      2014   6       ADCE
CA      2016   11      Wa

      

newcol

c DT2

is generated from the data DT1

.
I've seen the syntax how DT2[DT1, ...]

, but I forgot it. Any help?

Data

DT1 <- "  code    type  
1:        AH1     AM
2:        AS5     AM
3:        NMR     AM
4:        TOS     AM
5:        IP      AD
6:        CC      ADCE
7:        CA      Wa
"
DT1 <- read.table(text=DT1, header = T)
DT1 <- as.data.table(DT1)

DT2 <- "code    year   month  
1: AH1     2011   2
2: AH1     2011   5
3: AS5     2012   7
4: AS5     2012   6
5: AS5     2013   3
6: CC      2014   6
7: CA      2016   11
"
DT2 <- read.table(text=DT2, header =T)
DT2 <- as.data.table(DT2)

      

PS Also, there is a function in excel VLOOKUP

to solve it:

# Take first obs. as an example. 
DT2
code    year   month  
AH1     2011   2
# newcol is column D. So in D2, we type:
=VLOOKUP(TRIM(A1), 'DT1'!$A$2:$A$8, 2, FALSE)

      

UPDATE based on the comment in @ akrun's answer.
My original DT1

has 86 total. and DT2

has 451125 vol. I use @akrun's answer and DT2 is shortened to 192409. So strange. The DT2 $ code does not contain any NA. I do not know why.

length(unique(DT1$code1)) 
[1] 86
length(unique(DT2$code))
[1] 39

table(DT1$code1) 
AHI AHI002 AHI004 AHI005 AHS002 AHS003 AHS004 AHS005    AMR AMR002 AMR003 AMRHI3   CARD   CCRU  HPA01  HWPA1 HWPA1T    IOA  IOA01 
 1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1 
IOA01T IPA010 IPA011 IPA012 IPA013 IPA014 IPACC3 IPACC4 IPACC5 IPACC6   IPAR  IPAR2 IPARK2 IPARKI   NAHI  NAHI2   NAMR  NAMR2    NCC 
 1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1 
NCC2   NCC5  NCC5T  NNAHI NNAHI2  NNAMR NNAMR2     PL    PL2   PLFI    REI    SPA SPA001   SPA3   TADS  TADS2   TAHI  TAHI2   TAHS 
 1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1 
TAHS2   TAMB  TAMB2   TAMD  TAMD2   TAMR  TAMR2  TBURN TBURN2   TCCR   TFPS    TFS   TFS2    THE  THIBN THIBN2   TICU  TICU2   TIPA 
 1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1 
TIPA2  TIPAK TIPAK2   TNCC    TOS   TOS2   TSAO  TSAO2   TSPA    WED 
 1      1      1      1      1      1      1      1      1      1 

table(DT2$code)
AHI002 AHI005 AHS002 AHS005 AMR    AMR003 Card   HPA01  HWPA1  HWPA1T IOA01  IOA01T IPA011 IPA012 IPA013 IPA014 IPACC3 IPACC4 IPACC5 
19408  12215  34184  12226  19408  12215  19408   7344   9198    405   9198    405  12215   5137   1148   2853  31703   9198   7878 
IPACC6 IPAR   IPAR2  IPARK2 IPARKI NAHI   NAHI2  NAMR   NAMR2  NCC2   NCC5   NCC5T  NNAHI  NNAHI2 NNAMR  NNAMR2 PL     PL2    SPA    
9668  41909   9643   2362   2967  10018   3589  10018   3589   7878   2845    536  14776   8104  14754   8118  18624   8302  40856 
SPA3   
6823 

      

+3


source to share


2 answers


You can use merge

in R base:

DT2 <- (merge(DT1, DT2, by = 'code'))

      

Note. It also sorts it by column 'code'

.

You can also use the package plyr

:

DT2 <- plyr::join(DT2, DT1, by = "code")

      



Since you are interested in using the package data.table

:

library(data.table)
DT2 <- data.table(DT2, key='code')
DT1 <- data.table(DT1, key='code')

DT2[DT1]

      

Or a qdap

package:

DT2$type <- qdap::lookup(DT2$code, DT1)

      

+4


source


We can do this with a connection from data.table



library(data.table)
DT2[DT1, on = .(code), nomatch = 0]
#   code year month type
#1:  AH1 2011     2   AM
#2:  AH1 2011     5   AM
#3:  AS5 2012     7   AM
#4:  AS5 2012     6   AM
#5:  AS5 2013     3   AM
#6:   CC 2014     6 ADCE
#7:   CA 2016    11   Wa

      

+4


source







All Articles