merge two data.frame with condition in R

折月煮酒 提交于 2019-12-06 15:48:53

May be this helps

library(data.table)
setkey(setDT(df1),chr, Start, End)
setkey(setDT(df2), chr, Start, End)
res <- foverlaps(df1, df2,  type='any')[
   ( Start > i.Start| End> i.End)|is.na(Start)][,
   c('Start', 'End') := list(i.Start, i.End)][,7:8 := NULL]
dcast(res, ...~ID, value.var='Xp', fill=0)[, -7, with=FALSE]
#    chr Start  End    Gene S8411  S8413
#1:   4  1000 1105 Gm12724 0.312  0.000
#2:   4  1254 1369   Zfhx2 0.144 -1.999
#3:   4  1452 1564 Olfr978 0.144  0.000
#4:   7  5004 5412 Usp17lc 0.000  0.000
#5:   7  5698 5789  Lingo1 0.136  0.000
#6:   7  5987 6041   Sart3 0.136  0.000
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!