在SQL中,您可以像这样连接表:
...on table1.long_id like '%'+table2.short_id+'%'
其中long_id是一个长字符串,可能包含也可能不包含short_id,而%是通配符。
在R,最好是在data.table中有这样的加入方式吗?就像这样:
table1[table2, on = .(long_id %like% short_id)]
我认为这可能是非马甲加盟的一部分,但我想不出该怎么做。谢谢!
编辑
我在下面添加了一个可重复的例子。
在下面的两个数据表中,我想加入。
t1<- data.table(longid=c("5-6-7", "6-4-6", "4-1-5", "4-2-9", "2-8-6"))
t2<- data.table(shortid=c("1", "2", "3"))我希望结果是这样
result <- data.table(shortid=c("1", "2", "2"), longid=c("4-1-5", "4-2-9",
"2-8-6"))发布于 2018-02-07 17:21:46
你的数据文件:
t1<- data.table(longid=c("5-6-7", "6-4-6", "4-1-5", "4-2-9", "2-8-6"))
t2<- data.table(shortid=c("1", "2", "3"))使用grepl,我发现id已被接受
TF<-sapply(as.character(t2$shortid),grepl,x=as.character(t1$longid)) #Like operation using grepl在这里我找到了匹配的
max1<-apply(TF,1,function(x) which(x==1)) #Find match in t1
max2<-apply(t(TF),1,function(x) which(x==1)) #Find match in t2组合输出
out<-cbind(t1[rep(seq_len(nrow(t1)), unlist(lapply(max1,length))),],t2[rep(seq_len(nrow(t2)), unlist(lapply(max2,length))),]) #bind t1 and t2 matches
out
longid shortid
1: 4-1-5 1
2: 4-2-9 2
3: 2-8-6 2https://stackoverflow.com/questions/48669163
复制相似问题