R中数据集的偏移联接

R中数据集的偏移联接,r,dplyr,data.table,R,Dplyr,Data.table,有数据 data1=structure(list(y1 = c(3L, 5L, 6L, 7L, 5L), y2 = c(5L, 7L, 8L, 9L, 5L)), .Names = c("y1", "y2"), class = "data.frame", row.names = c(NA, -5L)) 它是两个变量(y1,y2) 实际上有5条线 1,2,3,4,5 还有另一个数据集 data2=structure(list(v1

有数据

data1=structure(list(y1 = c(3L, 5L, 6L, 7L, 5L), y2 = c(5L, 7L, 8L, 
9L, 5L)), .Names = c("y1", "y2"), class = "data.frame", row.names = c(NA, 
-5L))
它是两个变量(y1,y2)

实际上有5条线

1,2,3,4,5
还有另一个数据集

data2=structure(list(v1 = 1:2, v2 = c(1L, 3L), x = c(10L, 30L)), .Names = c("v1", 
"v2", "x"), class = "data.frame", row.names = c(NA, -2L))

v1  v2  x
1   1   10
2   3   30
v1为1,2,v2为1,3这是行数。 所以我必须加入v1的第一行和v2的第一行 y1的第一行和y2的第一行

然后 v1的第二行我必须与y1的第二行和v2的第三行连接,我必须与y2的第三行连接

所以输出

y1  y2  v1  v2  x
3   5   1   1   10
5   8   2   3   30
如果这篇文章是重复的,让我知道,我会删除它

让我把这篇文章编辑得更清楚些 数据2

这些变量中的每一个都表示必须与之联接到data1中的行

这里是数据1

      y1    y2

    `21     45
    q456    346
    q346    3q6
    yq      ewy
    wey     4e
    werer   yu
    ytu 256
    4323    62546u4
    ftyb    bynj
    dfgg    2335
    ye     4556
    1       2


V1 =1 must be joint with first row of y1
v2=1 must be joint with first row of y2
v1=3  must be joint with third row of y1
v2=5 must be joint with five row of y2
v1=4  must be joint with 4 row of y1
v2=8 must be joint with 8 row of y2
v1=7  must be joint with 7 row of y1
v2=9 must be joint with 9 row of y2
输出

v1  v2  y1  y2
1   1   `21 45
3   5   q346    4e
4   8   yq  62546u4
7   9   ytu bynj

如果我理解正确,
data2
包含
data1
中的行索引。OP希望通过分别在
v1
v2
给出的行中查找
y1
y2
的值来更新
data2

对于
data.table
这可以通过两种不同的方法解决

通过引用查找和更新 更新联接 这里,一个helper列附加到
data1
,其中包含要连接的行索引

资料
库(data.table)

data2如果我理解正确,
data2
包含
data1
的行索引。OP希望通过分别在
v1
v2
给出的行中查找
y1
y2
的值来更新
data2

对于
data.table
这可以通过两种不同的方法解决

通过引用查找和更新 更新联接 这里,一个helper列附加到
data1
,其中包含要连接的行索引

资料
库(data.table)

data2在这个例子中,
data2$y1@phiver,如何处理多个列?我不能总是手动操作?也许这是你想要的输出<代码>对于(序列中的i(ncol(data1)))data2[[names(data1)[i]]]]]@Ryan,它返回null),我将在5分钟内编辑post@Ryan,请选择编辑。在这个例子中,
data2$y1@phiver,如何处理多个列?我不能总是手动操作?也许这是你想要的输出<代码>对于(序列中的i(ncol(data1)))data2[[names(data1)[i]]]]]@Ryan,它返回null),我将在5分钟内编辑post@Ryan,请选择编辑。明白了吗
      y1    y2

    `21     45
    q456    346
    q346    3q6
    yq      ewy
    wey     4e
    werer   yu
    ytu 256
    4323    62546u4
    ftyb    bynj
    dfgg    2335
    ye     4556
    1       2


V1 =1 must be joint with first row of y1
v2=1 must be joint with first row of y2
v1=3  must be joint with third row of y1
v2=5 must be joint with five row of y2
v1=4  must be joint with 4 row of y1
v2=8 must be joint with 8 row of y2
v1=7  must be joint with 7 row of y1
v2=9 must be joint with 9 row of y2
v1  v2  y1  y2
1   1   `21 45
3   5   q346    4e
4   8   yq  62546u4
7   9   ytu bynj
library(data.table)
setDT(data1)
setDT(data2)
data2[, y1 := data1[v1, y1]]
data2[, y2 := data1[v2, y2]]
data2
   v1 v2   y1      y2
1:  1  1  `21      45
2:  3  5 q346      4e
3:  4  8   yq 62546u4
4:  7  9  ytu    bynj
library(data.table)
setDT(data2)[setDT(data1)[, rn := .I], on = .(v1 = rn), y1 := i.y1][
  data1, on = .(v2 = rn), y2 := i.y2]
data2
   v1 v2   y1      y2
1:  1  1  `21      45
2:  3  5 q346      4e
3:  4  8   yq 62546u4
4:  7  9  ytu    bynj
library(data.table)
data2 <- fread("
v1  v2
1   1
3   5
4   8
7   9
")

data1 <- fread("
y1    y2
`21     45
q456    346
q346    3q6
yq      ewy
wey     4e
werer   yu
ytu 256
4323    62546u4
ftyb    bynj
dfgg    2335
ye     4556
1       2
")