简体   繁体   English

加入 data.tables 时解析列名

[英]parsing column names when joining data.tables

I need to join the information of column h of dataframe Y into dataframe X .我需要将 dataframe Yh列信息加入到 dataframe X中。 The code below shows the desired output.下面的代码显示了所需的 output。

library(data.table)
X <- data.table(
  a1 = rep("A", 6), 
  b1 = rep(1,6), 
  c1 = rep(c(0,1), 1, each = 3),
  d = letters[1:6]
)

Y <- data.table(
  a2 = rep(c("A","B", "C"), 1, each = 2),
  b2 = rep(c(1, 2, 3), 1, each = 2),
  c2 = rep(c(0,1), 3),
  h = letters[7:12]
)


# final result

X[Y,
  on = .(a1 = a2, 
         b1 = b2, 
         c1 = c2),
  h := i.h
  ][]
#>    a1 b1 c1 d h
#> 1:  A  1  0 a g
#> 2:  A  1  0 b g
#> 3:  A  1  0 c g
#> 4:  A  1  1 d h
#> 5:  A  1  1 e h
#> 6:  A  1  1 f h

Created on 2020-08-03 by the reprex package (v0.3.0)reprex package (v0.3.0) 创建于 2020-08-03

The problem, however, is that the names of the columns that I use for making the join vary depending on the information stored somewhere else.然而,问题是我用于进行连接的列的名称会根据存储在其他地方的信息而有所不同。 So, let's assume that the name of the column c1 in X is stored in var , say var <- "c2" .因此,假设X中列c1的名称存储在var中,比如var <- "c2" Now, when I tried to do the join, nothing seems to work.现在,当我尝试加入时,似乎没有任何效果。

# None the attempts below works
var <- "c1"

# attempt 1
X[Y,
  on = .(a1 = a2, 
         b1 = b2, 
         eval(var) = c2),
  h := i.h
][]

# attempt 2
X[Y,
  on = .(a1 = a2, 
         b1 = b2, 
         get(var) = c2),
  h := i.h
][]

# attempt 3
cond    <- paste0(deparse(var), " = c2")
parcond <- parse(text = cond)

X[Y,
  on = .(a1 = a2, 
         b1 = b2, 
         eval(parcond)),
  h := i.h
][]

At the end, the only way I found to solve it is very inelegant, but it seems to be working.最后,我发现解决它的唯一方法非常不优雅,但它似乎有效。

var <- "c1"
setnames(X, var, "c2")

X[Y,
  on = c("a1" = "a2", 
         "b1" = "b2", 
         "c2"),
  h := i.h
][]

setnames(X, "c2", var)

However, I wonder if there is a better way to do this programmatically.但是,我想知道是否有更好的方法以编程方式执行此操作。

I checked all these links , but I could not find a solution that works for me.我检查了所有这些链接,但找不到适合我的解决方案。

Thank you so much for your help.非常感谢你的帮助。

Thanks to @chinsoon12 for his/her comment, the solution to the problem would be as follows,感谢@chinsoon12 的评论,问题的解决方案如下,

library(data.table)
X <- data.table(
  a1 = rep("A", 6), 
  b1 = rep(1,6), 
  c1 = rep(c(0,1), 1, each = 3),
  d = letters[1:6]
  )

Y <- data.table(
  a2 = rep(c("A","B", "C"), 1, each = 2),
  b2 = rep(c(1, 2, 3), 1, each = 2),
  c2 = rep(c(0,1), 3),
  h = letters[7:12]
  )


var <- "c1"

onkey <- c("a1==a2", "b1==b2",  paste0(var,"==c2"))

X[Y, 
  on=onkey, 
  h := i.h
  ][]
#>    a1 b1 c1 d h
#> 1:  A  1  0 a g
#> 2:  A  1  0 b g
#> 3:  A  1  0 c g
#> 4:  A  1  1 d h
#> 5:  A  1  1 e h
#> 6:  A  1  1 f h

Created on 2020-08-11 by the reprex package (v0.3.0)reprex package (v0.3.0) 创建于 2020-08-11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM