R 如何通过每个id的多个列联接表
我被迫删除了我的上一个主题,因为它没有被很好地询问,而且这个例子有点复杂,所以这里是一个简单的例子 我有两个数据帧:R 如何通过每个id的多个列联接表,r,join,grouping,R,Join,Grouping,我被迫删除了我的上一个主题,因为它没有被很好地询问,而且这个例子有点复杂,所以这里是一个简单的例子 我有两个数据帧: DF1-DF1 id1客户端\代码id2值1值2 1 x1 a 0.1 1.1 2 1 x1 b 0.2 1.2 3 1 x1 c 0.3 1.3 4 1 x2 d 0.4 1.4 5 1 x2 e 0.5 1.5 6 2 x3 y 0.6 1.6 DF2 DF2 id1 id2值1值2 1111020 2 1 b 11 21 3 1 c 12 22 4 1 d 13 23 5
DF1-DF1
id1客户端\代码id2值1值2
1 x1 a 0.1 1.1
2 1 x1 b 0.2 1.2
3 1 x1 c 0.3 1.3
4 1 x2 d 0.4 1.4
5 1 x2 e 0.5 1.5
6 2 x3 y 0.6 1.6
DF2 DF2
id1 id2值1值2
1111020
2 1 b 11 21
3 1 c 12 22
4 1 d 13 23
5 1 e 14 24
6 1楼15 25
72x1626
8 2 y 17 27
每个客户机都属于由列(id1)标识的一组客户机
我试图做的是添加DF2中缺少的行,这些行的id2在DF1中不存在。此过程应针对DF1中属于同一组客户机(id1)的每个客户机(客户机代码)执行
(我不知道我是否说得够清楚)
所需输出:
输出
id1客户端\代码id2值1值2
1 x1 a 0.1 1.1
2 1 x1 b 0.2 1.2
3 1 x1 c 0.3 1.3
4 1 x1 d 13.0 23.0
5 1 x1 e 14.0 24.0
6 1 x1 f 15.0 25.0
7 1 x2 d 0.4 1.4
8 1 x2 e 0.5 1.5
9 1 x2 10.0 20.0
101x2B11.021.0
11 1 x2 c 12.0 22.0
12 1 x2 f 15.0 25.0
13 2 x3 y 0.6 1.6
14 2 x3 x 16.0 26.0
提前感谢。首先,创建一个表,显示每个客户端的
id2
值应该是多少
出席:
库(tidyverse)
客户违约率%
不同(客户端代码,id1)%>%
左联合(df2,by=“id1”)
客户端默认设置
#>客户端代码id1 id2 value1 value2
#>1x1120
#>2 x1 1 b 11 21
#>3 x1 1 c 12 22
#>4 x1 1 d 13 23
#>5 x1 1 e 14 24
#>6 x1 1 f 15 25
#>7 x2 1 a 10 20
#>8x21b1121
#>9 x2 1 c 12 22
#>10 x2 1 d 13 23
#>11 x2 1 e 14 24
#>12 x2 1 f 15 25
#>13 x 3 2 x 16 26
#>14 x 3 2 y 17 27
然后,删除df1
中已经存在的行,并添加其余行:
客户端\u缺失%
反连接(df1,by=c(“客户机代码”,“id2”))
绑定行(df1,缺少客户端)%>%排列(客户端代码)
#>客户端代码id1 id2 value1 value2
#>1 x1 1 a 0.1 1.1
#>2 x1 1 b 0.2 1.2
#>3 x1 1 c 0.3 1.3
#>4x11d13.023.0
#>5x11e14.024.0
#>6 x1 1 f 15.0 25.0
#>7 x2 1 d 0.4 1.4
#>8 x2 1 e 0.5 1.5
#>9 x2 1 a 10.0 20.0
#>10x21b11.021.0
#>11 x2 1 c 12.0 22.0
#>12 x2 1 f 15.0 25.0
#>13 x3 2 y 0.6 1.6
#>14 x 3 2 x 16.0 26.0
数据:
df1另一个选项是检查DF2中的每一行
我们重新创建您的表,并将一些列设置为.character:
library(data.table)
DF1<-data.frame(id1=c(1,1,1,1,1,2),client_code=c("x1","x1","x1","x2","x2","x3"),id2=c("a","b","c","d","e","y"),value1=c(0.1,0.2,0.3,0.4,0.5,0.6),value2=c(1.1,1.2,1.3,1.4,1.5,1.6))
DF1$id2 <- as.character(DF1$id2)
DF2<-data.frame(id1=c(1,1,1,1,1,1,2,2),id2=c("a","b","c","d","e","f","x","y"),value1=c(10,11,12,13,14,15,16,17),value2=c(20,21,22,23,24,25,26,27))
DF2$id2 <- as.character(DF2$id2)
库(data.table)
DF1这里是一个可能的data.table解决方案,它更简洁一些。基本上,它执行两个步骤:
通过在id1
和client\u code
中的每组上连接DF1
和DF2
来构建一个完整的data.table
通过相应的DF1
值更新value1
和value2
列
库(data.table)
setDT(DF1);setDT(DF2)
DF id1客户端\代码id2值1值2
#>1:1 x10.11.1
#>2:1 x1 b0.21.2
#>3:1x1C0.311.3
#>4:1 x1 d 13.0 23.0
#>5:1x14.024.0
#>6:1 x15.0 25.0
#>7:1x210.020.0
#>8:1x2B11.021.0
#>9:1x2C12.022.0
#>10:1 x2 d 0.4 1.4
#>11:1 x2 e 0.5 1.5
#>12:1 x2 f 15.0 25.0
#>13:2x16.026.0
#>14:2x3 y 0.61.6
由(v0.3.0)创建于2019-07-01这里是一个使用sql查询的解决方案
library(sqldf)
sqldf('
select distinct
a.id1
, a.client_code
, b.id2
, coalesce(d.value1, e.value1) as value1
, coalesce(d.value2, e.value2) as value2
from df1 a
left join df2 b
on a.id1 = b.id1
left join df1 d
on a.id1 = d.id1
and b.id2 = d.id2
and a.client_code = d.client_code
left join df2 e
on a.id1 = e.id1
and b.id2 = e.id2
')
# id1 client_code id2 value1 value2
# 1 1 x1 a 0.1 1.1
# 2 1 x1 b 0.2 1.2
# 3 1 x1 c 0.3 1.3
# 4 1 x1 d 13.0 23.0
# 5 1 x1 e 14.0 24.0
# 6 1 x1 f 15.0 25.0
# 7 1 x2 a 10.0 20.0
# 8 1 x2 b 11.0 21.0
# 9 1 x2 c 12.0 22.0
# 10 1 x2 d 0.4 1.4
# 11 1 x2 e 0.5 1.5
# 12 1 x2 f 15.0 25.0
# 13 2 x3 x 16.0 26.0
# 14 2 x3 y 0.6 1.6
你好显然这还不清楚。如果你不能清楚地表达你的意思,那么你就不能对你想要的东西进行推理、交流、搜索或编码。例如,在“完成DF2中缺少的行”中,“缺少”和“完成”是无法解释的,并且D2中没有缺少完成的行。这只是一堆出现在清晰规范中的单词。此外,根据给出的信息猜测,这显然是一个常见问题。PS在给出关系(ship)/关联或表(base或query result)时,请说明其中的一行根据其列值表示的业务状况。
client_codes <- as.character(unique(DF1$client_code))
New_Table <- data.frame()
for(i in client_codes){
#So here we subset the DF1 data frame to only a certain client code
New_DF1 <- DF1[DF1$client_code == i,]
temp_id <- unique(New_DF1$id1)
#and here we select the correct ids
New_DF2 <- DF2[DF2$id1 %in% temp_id,]
temp_df <- data.frame()
#And with this second for loop we perform a check on each of the new_DF2 rows
#to see if they have a matching client id
for(j in 1:nrow(New_DF2)){
temp_row <- New_DF2[j,]
if(nrow((New_DF1[New_DF1$id1 == temp_row$id1 & New_DF1$id2 == temp_row$id2,])) == 0){
temp_row$client_code <- i
setcolorder(temp_row, column_order)
temp_df <- rbind(temp_df, temp_row)
}
}
New_DF1 <- rbind(New_DF1, temp_df)
New_Table <- rbind(New_Table, New_DF1)
}
library(sqldf)
sqldf('
select distinct
a.id1
, a.client_code
, b.id2
, coalesce(d.value1, e.value1) as value1
, coalesce(d.value2, e.value2) as value2
from df1 a
left join df2 b
on a.id1 = b.id1
left join df1 d
on a.id1 = d.id1
and b.id2 = d.id2
and a.client_code = d.client_code
left join df2 e
on a.id1 = e.id1
and b.id2 = e.id2
')
# id1 client_code id2 value1 value2
# 1 1 x1 a 0.1 1.1
# 2 1 x1 b 0.2 1.2
# 3 1 x1 c 0.3 1.3
# 4 1 x1 d 13.0 23.0
# 5 1 x1 e 14.0 24.0
# 6 1 x1 f 15.0 25.0
# 7 1 x2 a 10.0 20.0
# 8 1 x2 b 11.0 21.0
# 9 1 x2 c 12.0 22.0
# 10 1 x2 d 0.4 1.4
# 11 1 x2 e 0.5 1.5
# 12 1 x2 f 15.0 25.0
# 13 2 x3 x 16.0 26.0
# 14 2 x3 y 0.6 1.6