R data.table join: SQL “select *” alike syntax in joined tables?

前端 未结 3 1700
-上瘾入骨i
-上瘾入骨i 2020-12-19 04:24

I have two data.tables with many fields.

I want to join the two tables, add some calculated fields and append all other fields from the first, second or both tables

3条回答
  •  臣服心动
    2020-12-19 04:49

    This should precisely answer your need.
    It uses very powerful R feature called computing on the language (or meta programming) well described in official R Language Definition manual. This is an exceptional feature of R language and should not be forgotten IMO.

    library(data.table)
    DT1 = data.table(x=c("c", "a", "b", "a", "b"), a=1:5)
    DT2 = data.table(x=c("d", "c", "b"), b=6:8)
    
    jj = as.call(c(
        list(as.name(".")),
        list(sum = quote(a+b)),
        lapply(unique(c(names(DT1), names(DT2))), as.name)
    ))
    print(jj)
    #.(sum = a + b, x, a, b)
    DT1[DT2, eval(jj), on="x"]
    #   sum x  a b
    #1:  NA d NA 6
    #2:   8 c  1 7
    #3:  11 b  3 8
    #4:  13 b  5 8
    

提交回复
热议问题